Combobox Rowsource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alireza355
    New Member
    • Feb 2009
    • 86

    Combobox Rowsource

    Dear friends,

    I have 2 comboboxes in a form. I want the first combobox to show all records of my table if the second combobox is null.

    But if the second combobox is not null, I want the first combobox to show only information which contains the selected item in the second combobox.

    Example:

    field1 field2
    alfa 1
    bravo 1
    charlie 1
    david 2
    echo 2
    fox 2
    golf 2
    honey 3
    item 3
    jack 3

    I want the first combobox to show all the "field1" items if the second combobox is null, and if the second combobox is for example "2", I want the first combobox to show only "david", "echo", "fox" and "golf"

    Thank you.
  • damonrulz
    New Member
    • Sep 2008
    • 13

    #2
    This process is called cascading combo boxes. There is a tutorial here but I have made my own tutorial specific to your needs which is listed below:
    1. Firstly create two tables:

      Code:
      TableNumbers 
      [ID] – AutoNumber, PK 
      [Number] – Number, This is your 1, 2, or 3
        
      TableWords
      [ID] – Autonumber, PK
      [Word] – Text, This is your Alfa, Bravo, Charlie, ect 
      [NumID] – FK, Used to link the word to the number
      Now enter your values you stated before (1, 2 & 3 into TableNumbers, and Alfa, Bravo, Charlie, ect with the corresponding numbers into the TableWords).


    2. Then on the form where you want the combo boxes, you need to create the combo box containing the numbers. Create a combo box (without wizard) called "Combo1":

      Row Source – TableNumbers

      Column Count – 2 (We use 2 columns because we want to include both ID and Number)

      Column Widths – 0cm;2cm (We set the first column to 0” because the user does not need to see the ID)

      Bound Column – 1 (We bind it to the first column so that when we refer to [Combo1], it will return the ID rather than Number)

      Then create another combo called "Combo2" but leave this one blank the coding later will put the data into it.


    3. What we want is to change the Row Source of [Combo2] whenever the user makes a change of selection to [Combo1].

      So, in the After Update event property of [Combo1], you’ll want to change it to [Event Procedure].

      Then, click on the "..." button to bring up the Visual Basic Editor. In the module, put in the following code:
      Code:
      Private Sub Combo1_AfterUpdate()
        With Me![Combo2]
            If IsNull(Me!Combo1) Then
            .RowSource = "SELECT [Word] " & _
                         "FROM TableWords "
            Else
            .RowSource = "SELECT [Word] " & _
                         "FROM TableWords " & _
                         "WHERE [NumID]=" & Me!Combo1
          End If
          Call .Requery
        End With
        Forms!Form1!Combo2 = ""
      End Sub


    Now when 1 is selected in Combo1, Alfa, Bravo & Charlie will be in Combo 2.

    When 2 is selected, David, Echo, Fox & Golf will appear.

    When 3 is selected Honey, Item & Jack will appear.

    When Combo 1 is blank, all of the values appear in Combo2.

    Comment

    • Alireza355
      New Member
      • Feb 2009
      • 86

      #3
      Thanx a lot

      Thanx a lot.

      It works...

      Comment

      Working...