
Populating a combo box from another combo box
Chas,
You don't say how you are accessing the Access database, so I'll use the
Data Object.
I will assume that Combo1 holds a list of unique items that can reliably be
used for a search.
Private Sub Combo1_Click()
Dim criteria As String
criteria = "MyField = '" & Combo1.Text & "'" ' note: Single quotes
used for text. If numeric, then no single-quotes.
Combo2.Clear
Data1.Recordset.FindFirst criteria
Do Until Data1.Recordset.NoMatch = True
Combo2.AddItem Data1.Recordset.Fields("MyField")
Data1.Recordset.FindNext criteria
Loop
End Sub
If your list might have duplicate entries in Combo1 and thus be prone to
search errors on the text, you might look into the ItemData property of
ComboBoxes and ListBoxes. It is a Long Integer that is associated with each
item in the list. If, when you fill Combo1, you also add a Long primary key
(such as an Autonumber) to the item's ItemData then each item will have a
unique identifier for searching.
Private Sub Combo1_Click()
Dim criteria As String
criteria = "MyNumericField = " & Combo1.ItemData(Combo1.ListIndex)
Combo2.Clear
Data1.Recordset.FindFirst criteria
Do Until Data1.Recordset.NoMatch = True
Combo2.AddItem Data1.Recordset.Fields("MyField")
Data1.Recordset.FindNext criteria
Loop
End Sub
I hope that this helps. If not, post again.
Michael
Quote:
> Hello,
> I'm a relatively new programmer and I'm trying to figure out how to
use
> my selection in a ComboBox1 lead to filling my second combo box. I am
doing
> this in VB code from an Access 2000 data base. The Database has two tables
> and after selecting combobox1 from my choice in the first table I want my
> second combo box to fill with the selections on table two that correlate
> with my table one selection. Thanks for your time and some sample code
would
> help.
> Chas