January 5th, 2000, 05:59 AM
Dear All,
I am a beginner in VB6. I am looking to populate a combo box with sql
server data using ADO connection. And then on the basis of selection to
retrieve, update or delete data in the sql server 7 db. If anybody can
help providing a sample code.
thanks in anticipation.
valkyrie
January 5th, 2000, 10:29 PM
Hi...
I've done this using VB5 and SQL6.5 with ADO2.1, so the code should be quite similar.
Anyway, I'll write this in pseudocode. I am connecting to the SQL server during the Form_Load routine, but the event of populating my combobox only occurs at runtime, when the combobox_dropdown() event is fired.
private Sub Combo1_DropDown()
'Declare some local variables
'1) one for the loop counter
'2) one for the ADO Recordset object
'3) one string variable to construct an SQL string
'Clear the combobox...
Combo1.Clear
'set the recordset object, for example:
set rstOne = new ADODB.Recordset
'Build the SQL string and store it in the string variable, for example:
strSQL = "SELECT * FROM books"
'Open the recordset with the SQL string, the ADO connection,
'open mode, lock mode, command type
rstOne.Open YourSelectString, YourConnection, adOpenStatic, adLockOptimistic, adCmdText
'Populate the combobox in a loop
for ctr = 1 to rstOne.RecordCount step 1
Combo1.AddItem rstOne.Fields("authors") 'Add data from the author field into the combobox
rstOne.MoveNext 'Move the recordset
Loop
set rstOne = nothing 'dereference your recordset...
End Sub
I would say that this isn't the best way to solve your problem, but if you have any better ideas, please feel free to share it with the forum!
Hope this helps...
____________________________________
The VB Bugs in my Life...