Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fill combo box

Status
Not open for further replies.

nirs

IS-IT--Management
Apr 4, 2003
37
IL
is there a way to fill a combo box from a recordset
in other way other then additem method
filling must be in run time and not in design time

thanks
 
You can databind the control to the recordset, have a look at online help entitled "Databinding in Visual Basic"

"Own only what you can carry with you; know language, know countries, know people. Let your memory be your travel bag.
 
I am not fond of data binding as a rule for several reasons.
My suggestion would be to open a recordset forward only and read only, then loop through the recordset using the additem method

This example uses a SQL Server stored procedure that receives input parameters and returns a record set so it is a bit more complex than just using a simple select but should give you the idea.

Note: the Data Base connection is already open. Depending on your app you may want to do that differently

Private Sub FillCboCombo()
Dim Rs As New ADODB.Recordset

Dim strS As String
Dim cmd As New Command
Dim Param As New Parameter

'note: although there is a less verbose means of doing this code, the less verbose method fails when attempting to open a recordset. You must use this syntax when returning a
'value from a stored procedure

With cmd
.CommandText = "sp_FillCombo"
.CommandType = adCmdStoredProc
.ActiveConnection = DBConn
Set Rs = .Execute
End With
Do While Not Rs.EOF And Not Rs.BOF
cboTable.AddItem Rs!NameOfColumn
Rs.MoveNext
Loop
cboComb0.Text = cboCombo.List(0)
Set Rs = Nothing

End Sub
 
I would agree I am not a fan of databinding, though the request was how to do it without using AddItem method.


"Own only what you can carry with you; know language, know countries, know people. Let your memory be your travel bag.
 
correct

I misread the origional post. I thought the question was other than not using the add item method at design time.

You are, of course correct that to avoid the add item method totally (but why????) then you must use databinding as far as I know.
 
The trouble with data binding is that you have to trust Microsoft to have it work properly. Meaning many hours researching workarounds in forums like these. I prefer to go as low level as possible because I trust my code more than theirs. At least it's easier to debug. For example, if you use the DataRepeater control, you HAVE to use data binding. There's a bug in it that causes it to fail to place your pointer in the right places in some cases, and the workaround is to bind an additional ADODC control to it.

Kludge, kludge, kludge....

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top