How to connect a database to crystal report at run time? - vb6

I am developing winform application in vb6. I am using crystal report 4.6. I have created a crystal report which shows all data from a table (MS Access). And I unchecked save data with report and i saved the report. I just want to invoke it in application. So I included the component CrystalReportControl in my application. Now i want to set the records to be displayed in the report. The records are selected according to the user input to the text box.
Records are retrived from the database is done in following code.
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub Command1_Click()
Set conn = New ADODB.Connection
conn.Open "provider=Microsoft.jet.oledb.4.0;Data Source=" & App.Path &"\faculty.mdb"
Set rs = New ADODB.Recordset
rs.Open "select * from facultydetails where eid=1234", conn, adOpenDynamic, adLockPessimistic
CrystalReport1.ReportFileName = App.Path & "\faculty.rpt"
Set CrystalReport1.DataSource = rs
CrystalReport1.Action = 1
End Sub
Gives an error for the line: Set CrystalReport1.DataSource = rs :
as Property is write-only.
Tell me how the records of the report can be dynamic? Plz help me...

Instead of
Set CrystalReport1.DataSource = rs
do
CrystalReport1.DataSource = rs
EDIT:
Take a look at the following example and see if that will help you:
'CrystalReport1 is the name of the dsr file
Dim Report As New CrystalReport1
Dim cdoRowset As CrystalDataObject.CrystalComObject
Dim varArray() As Variant
'Open ADO Connection
Set m_cnAdo = New ADODB.Connection
m_cnAdo.ConnectionString = "DRIVER={SQL Server};UID=[UserID];PWD=[Password]" _
& ";SERVER=[Server];DATABASE=[Database]"
m_cnAdo.Open
Dim rsAdo As ADODB.Recordset
Dim cmdAdo As ADODB.Command
'Using Embedded Query
Set cmdAdo = New ADODB.Command
Set rsAdo = New ADODB.Recordset
cmdAdo.ActiveConnection = m_cnAdo
cmdAdo.CommandText = "SELECT * FROM Table WHERE Param = " & lngParam1
cmdAdo.CommandType = adCmdText
Set rsAdo = cmdAdo.Execute
Report.Database.SetDataSource rsAdo, 3, 1

Related

Getting Run-time error '3706' error while connecting to Oracle DB using VBA in excel

I am trying to connect to Oracle DB for the first time using the ODBC Datasource Connector(named Demo_DSN) When I Step Into (F8) the code is giving Run-time error '3706': application-defined or object -defined error
Sub ConnectToOracle()
Dim cn As ADODB.Connection
Dim rs As ADODB.RecordSet
Dim mtxdata As Variant
Set cn = New ADODB.Connnection
Set rs = New ADODB.Recordset
cn.Open ( _
"User ID = XXGMDMADM" & _
";Password=xxgmdmadmdb" & _
";Data Source= Demo_DSN" &_
";Provider= OraOLEDB.Oracle")
'Cleanup in the end
Set rs = Nothing
Set cn = Nothing
End Sub

Not returning any result while retrieving data from oracle database using vb6, please help me to locate the error

When i check the result set. record count it returns -1 and while checking the recordset.EOF it returns true, thus the result set does not contain any value.
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL
Private Sub cmd_login_Click()
Dim pass As String
con.ConnectionString = "Provider=msdaora;Data Source=localhost;User Id=ams;Password=krishnan;"
con.Open
strSQL = "Select passwrd from ams.login_details where username = 'Admin'"
rs.Open strSQL, con
If Not (rs.EOF) Then
If rs("passwrd") = txt_pass.Text Then
MsgBox rs("passwrd")
End If
End If
rs.Close
con.Close
End Sub
I forget to commit the statements in Oracle Sql Developer that's why not data was fetched from the database, When i executed the commit statement, it's working fine.

Excel is being closed while connecting to Oracle using VBA

I am trying to connect to Oracle using Excel VBA but when the code is run, Excel is closed and reopened at line MsgBox rs.recordCount
Sub Ora_Connection()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim query As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
On Error GoTo final1
query = "Select * from EMP where EMP_NO='998234'"
strcon = "Driver={Oracle in OraClient11g_home1};Dbq=smqa;Uid=MyUserID;Pwd=MyPassword;"
con.Open (strcon)
rs.CursorType = 1
rs.Open query, con
rs.MoveLast
MsgBox rs.recordCount
rs.Close
Set s = Nothing
con.Close
Set con = Nothing
final1:
MsgBox err.Number & " " & err.Description
End Sub
I am running this code on Windows 7 and trying to connect Oracle 11g. Do I need any additional setup to connect Oracle Db using Excel VBA or is something wrong in the code?

Populating VB6 Combo box using Resultset data source

How to Populate VB6 Combo box using Result set data source ... Please Help
Dim con As New ADODB.Connection
Dim rs2 As New ADODB.Recordset
con.Open "Provider = sqloledb;Data Source=Server01;Initial Catalog=Naveen; User ID= ****; password= ****; Integrated Security= True"
rs2.Open "Select * from Customers", con, adOpenDynamic
Do While rs2.EOF <> True
Combo2.AddItem (rs2.Fields(0).Value)
rs2.MoveNext
Loop
I'm not sure if it works the same way as in VB.NET so I would suggest that you look the ADODB.Recordset object and add each item to the combobox.
One way to load data from an Access database into a combo box (change the connection string for a different DB):
Dim oDb As New ADODB.Connection
Dim oRS As New ADODB.Recordset
Dim sSql As String
oDb.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\Data\MyAccessDB.mdb;Jet"
sSql = "SELECT DISTINCT([LastName]) FROM [Authors] ORDER BY LastName ASC"
oRS.Open sSql, oDB, adOpenForwardOnly, adLockReadOnly
Do While not oRS.EOF
With cboMyCombo
.AddItem trim$(oRS("LastName").Value)
End With
oRS.MoveNext
Loop
oRS.Close
oDB.Close
Set oRS = Nothing
Set oDB = Nothing

VB6 Get List of Active Directory Domains

Using VB6, is it possible to get a list of all available domains in active directory?
Thanks,
Alex
Add references for ActiveDS type library, and ADO to your project.
Sub GetDomains()
Dim objRootDSE As IADs
Dim objBase As IADs
Dim path As String
Dim rsDomains As ADODB.Recordset
Dim cnADS As ADODB.Connection
Dim cmdCommand As ADODB.Command
Set objRootDSE = GetObject("LDAP://rootDSE")
path = "LDAP://" & objRootDSE.Get("rootDomainNamingContext")
Set objBase = GetObject(path)
Set cnADS = New ADODB.Connection
cnADS.Provider = "ADsDSOObject"
cnADS.Open "ADSI"
Set cmdCommand = New ADODB.Command
cmdCommand.ActiveConnection = cnADS
cmdCommand.Properties("searchScope") = ADS_SCOPE_SUBTREE
cmdCommand.CommandText = "SELECT Name, distinguishedName FROM '" & objBase.ADsPath & "' WHERE objectCategory = 'domain'"
Set rsDomains = cmdCommand.Execute
Do While rsDomains.EOF = False
List1.AddItem (rsDomains!Name)
rsDomains.MoveNext
Loop
End Sub
I have only the one domain to test this against so I hope you'll need to let me know if it gets all the domains for you. Also please note, I didn't add error handling.

Resources