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!

Populating Combo Box from a Table in SQL Server 2

Status
Not open for further replies.

awelch

Technical User
Apr 24, 2002
85
US
Does anyone have the code to populate a combo box, based on the contents of a table in SQL server? I have searched throughout this site and many books, but am unable to pin down this solution. This is such an easy thing to do in Access, but I am stumped in VB.

Table name: Manufacturers
Fields: ManufacturerName, Model, Category

I am wanting to populate this combo box with the Category.

Thanks in advance for any suggestions.

Andrea [dazed]
 
Public DBConn As ADODB.Connection
Public DBComm As ADODB.Command
Public DBRec As ADODB.Recordset

Set DBConn = New ADODB.Connection
Set DBComm = New ADODB.Command
Set DBRec = New ADODB.Recordset
DBConn.ConnectionString = "Your Connection"
DBConn.Open
DBComm.ActiveConnection = DBConn
DBComm.CommandText = "Select Category from Table"
DBComm.CommandType = adCmdText
Set DBRec.Source = DBComm
DBRec.Open
If Not DBRec.EOF Then DBRec.MoveFirst
Do While Not DBRec.EOF
Combo.AddItem DBRec("Category")
DBRec.MoveNext
Loop
DBRec.Close
 
Thank you! That works great. [smarty]

Andrea
 
Does same one knows how to Retrieve .JPG from Access 2000 table (Data type - OLE object) and display it in Access 2000 Form
 
Hi,

Try posting you question in the access forum.

There is nothing wrong with the code above, but the command object is not necessary (neither is the connection actually):
-----------------------------------------------------------
Dim Rst as recordset

Set Rst = new recodset
Rst. open "Select Category from Table", "YourConnectionString"
If not Rst.eof and not Rst.bof then
rst.movefirst
Combo1.clear
while not rst.oef
combo1.add item Rst.fields("Category")
wend
else
msgbox "no matches"
end if
Rst.close
Set Rst = nothing
-----------------------------------------------------------
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top