Populating a combo box from another combo box 
Author Message
 Populating a combo box from another combo box

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



Sun, 16 Feb 2003 03:00:00 GMT  
 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



Sun, 16 Feb 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Populate a combo box from another combo box selection

2. Unable to populate text box from combo box

3. populate text box from combo box (same form)

4. enter data in a combo box based on value from another combo box

5. Limit subform combo box by selection in parent combo box

6. Want Access combo box to filter rowsource of another combo box

7. selecting records for 2nd combo box dependant on first combo box

8. Help filling second combo box from fist combo box

9. Combo box selection narrows next combo box choices

10. load 2nd combo box - depend on first combo box

11. Setting Combo box ListIndex initiates Combo box Click event

12. combo box/database combo box vlaue question - newbie

 

 
Powered by phpBB® Forum Software