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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you populate a combo box with a filed from a database

Status
Not open for further replies.

Matrix03

Programmer
Apr 9, 2001
14
CH
I have a table called Customer, with an attribute called CustomerLastName. How do I populate the combo box with the last names from the database into the combo box.
 
Here is a snippet from one of my own projects. rsCustomers is the recordset. The combo box is named cboCustomers. Feel free to ask for more help if you need.


With rsCustomers
Do Until .EOF
cboCustomers.AddItem !CustName
cboCustomers.ItemData(cboCustomers.NewIndex) = !CustID
.MoveNext
Loop
.MoveFirst
cboCustomers.Text = !CustName
End With

David Paulson


 
There is a way to do it without any code. Just put a DBCombo box on the form and in the Data Source you put the source of your data, whether it's a data control or SQL Statement or tablename. Your row source is the name of the field that you want displayed.
 
Do I need to open a connection to the database before this can work? Can you really break this down for me?
 
Here is a breakdown to Omega36 solution without writing any code.

1. Add a datacontrol to your form.
2. For its connect string property, connect to the database that you are using.
3. For its Rowsource property, select the Customer table.
4. Now, set the Data Source property of you combo box to the datacontrol.
5. If all the above steps were correct, you should see a list of the fields from the Customer table in a drop-down list of the rowsource property for the combo box. Now, like Omega36 said, set the rowsource property of the combo box to the CustomerLastName.

Hope its clear and correct.

Mirak
___________

If God be for us, who can be against us.....
 
yes but data binding is not always the way to go, if you populate the combo box with code you can have more control over it. but you can do it either way. ----------------
Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top