Populating VB6 Combo box using Resultset data source - vb6

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

Related

Use ADODB Recordset to INSERT Oracle DB

I am converting some Excel-VBA code that uploaded a DAO recordset to an Access database. My new code uses ADODB objects and needs to push the data to Oracle 12c.
I reviewed some articles, and found a handy equivalency chart here: From-DAO-to-ADO. Using this information I created the following code.
This first bit just loads up the source recordset. No problems here, but published if it's relevant:
Dim CN As New ADODB.Connection, RS As New ADODB.Recordset
Dim SRC_CN As New ADODB.Connection, SRC_RS As New ADODB.Recordset, SRC_CMD As New ADODB.Command
strSQL = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Src_WB_nm & _
";Extended Properties='Excel 12.0 Xml;HDR=NO';"
SRC_CN.Open strSQL
Set SRC_CMD.ActiveConnection = SRC_CN
SRC_CMD.CommandType = adCmdText
SRC_RS.Close
Set SRC_RS = Nothing
strSQL = "SELECT * FROM [" & TableNm & "]"
SRC_CMD.CommandText = strSQL
With SRC_RS
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Open SRC_CMD
End With
In this segment, I open the destination connection and attempt to open the destination recordset. Thismethodology (AddNew, RS...value = RS.value, RS.Update...) worked when I was in DAO. I expect it may need to be modified somewhat, but its the RS.Open command that I can't get past now.
CN.Open CSTRG
strSQL = "DELETE FROM DFSTOOL.C_QUERYLIST"
Set RS = CN.Execute(strSQL)
Set RS = Nothing
RS.Open "DFSTOOL.C_QUERYLIST", CN, adCmdTable
Do Until SRC_RS.EOF
RS.AddNew
RS.Fields(0).Value = SRC_RS.Fields(0).Value
RS.Update
SRC_RS.MoveNext
Loop
RS.Close
SRC_RS.Close
Set RS = Nothing
Set SRC_RS = Nothing
The error thrown is:
I appreciate any help you can provide!
So happily this was a simple syntax issue. Perhaps I should delete the post, but I'll leave it for now in case it helps anyone else. The equivalency table I referenced noted that the "adCmdTable" is an option in ADO, whereas the equivalent "dbOpenTable" was a type in DAO. Thus, I was required to skip a few fields so that it was in the right location. Other fine-tuning followed and the final iteration works as expected:
RS.Open "DFSTOOL.C_QUERYLIST", CN, adOpenForwardOnly, adLockOptimistic, adCmdTable
Do Until SRC_RS.EOF
RS.AddNew
RS.Fields(1).Value = SRC_RS.Fields(0).Value
RS.Update
SRC_RS.MoveNext
Loop
Thanks for your patience community!

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.

Trouble populating combobox from sql table

Need some minor help. I'm having an issue was as my combobox won't populate with the table I'm referencing too. Here's my code:
Public Function GetCodeDesc(sCode As String) As String
Dim strSQL As String, strAnswer As String
Dim objGetInfo As New ADODB.Recordset
strSQL = "SET ANSI_NULLS OFF"
Set objGetInfo = New ADODB.Recordset
objGetInfo.Open strSQL, objConnection, adOpenForwardOnly, adLockOptimistic
strSQL = "SELECT DISTINCT * FROM SH_RAWOCODES WHERE CODEX='" & sCode & "' ORDER BY CODEX"
Set objGetInfo = New ADODB.Recordset
objGetInfo.Open strSQL, objConnection, adOpenForwardOnly, adLockOptimistic
If Not objGetInfo.EOF And Not objGetInfo.BOF Then
strAnswer = objGetInfo.Fields(28)
End If
strSQL = "SET ANSI_NULLS ON"
Set objGetInfo = New ADODB.Recordset
objGetInfo.Open strSQL, objConnection, adOpenForwardOnly, adLockOptimistic
GetCodeDesc = strAnswer
End Function
Can anyone tell me where I might've went wrong?
You are not showing any code that adds items to a combobox so I am guessing that you don't have that. I also don't see in your sample where you expect to have more than 1 record returned so I am going to offer a generic put things into a combobox routine.
If objGetInfo.EOF = False And objGetInfo.BOF = False Then
objGetInfo.MoveFirst 'redundant, but just to be sure we're on the first record
Combobox1.Clear 'clear any current items
Do While objGetInfo.EOF = False
Combobox1.AddItem objGetInfo.Fields("MyField").Value
objGetInfo.MoveNext 'move to the next record
Loop
End If

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

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

Search for string using VBscript and copy that column to another worksheet

I have Book1.csv and Book2.xlsx. Book1.csv has many columns with data. Each column has unique title in first ROW. I need to find column with title “Processor Time” and copy all available data in this column to column1 in Book2.xlsx using VBscript. Please help.
You can use ADO to get the column:
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Docs\;"
strcon = strcon & "Extended Properties=""Text;FMT=Delimited;HDR=Yes;IMEX=1"";"
cn.Open strcon
strSQL = "Select [Processor Time] From [Book1.csv]"
rs.Open strSQL, cn
MsgBox rs.GetString
You can use automation with Excel:
Set xl = CreateObject("Excel.Application")
You can write to Excel from a recordset with:
xl.Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs
You can post back if you have problems putting the bits together.
EDIT re Comment
Try switching your code around a little:
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set objWorkbook1=xl.Workbooks.Open("C:\Docs\book2.xlsx")
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Docs\;"
strcon = strcon & "Extended Properties=""Text;FMT=Delimited;HDR=Yes;IMEX=1"";"
cn.Open strcon
strSQL = "Select [Processor Time] From [Book1.csv]"
rs.Open strSQL, cn
objWorkbook1.Worksheets("Sheet1").Cells(2, 1).CopyFromRecordset rs

Resources