how to add query result to combobox?

04-14-2004, 08:31 PM
for example,rst is the recordset after query,only one column,how can i add the record in rst to a combo box? thanks! :huh:

04-14-2004, 11:16 PM
You can just loop through the recordset and add each item to your combobox - this code assumes you have a text field:

Do While NOT rst.EOF
If NOT IsNull(rst!MyField)
Combo1.AddItem rst!MyField
End If

04-15-2004, 06:18 AM
It's faster to assign the recordset to an array and then fill the combo box from the array.
Dim varRecords As Variant
Dim intRows As Integer
Dim i As Long

intRows = rs.RecordCount ' see how many records (rows)
varRecords = rs.GetRows(intRows) ' retrieve all of the records

' loop through the records (rows) and fill the combobox
For i = 0 To intRows - 1
cbxTest.AddItem varRecords(0, i)
Next i

04-15-2004, 06:26 AM
Actually in this case, I don't agree :). I think building an array in this case is just added overhead. Since you don't really need to store this information in a UDT, or randomize the results (some quick sorting) I think just looping the recordset and adding each item is fine. The overall speed of looping the recordset is almost the same as looping the array..since they both will have the same amount of values.

04-15-2004, 08:14 PM
thanks to all of you

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum