Results 1 to 3 of 3

Thread: [RESOLVED] Combo box items

  1. #1

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Resolved [RESOLVED] Combo box items

    Please help! (And I apologise if this seems overly long).

    I am working on a userform in Word 2003. Amongst other things, the form contains several combo boxes. When the form is initialised:

    - Items A and B are added to ComboBox1
    - Items X, Y, Z, P, Q and R are added to ComboBox2.

    Now I am trying to restrict the list of items from ComboBox2 depending on the choice made from ComboBox1. For example if item A is selected in ComboBox1, I would like only X, Y and Z to be available from ComboBox2. But if item B is selected in ComboBox1, I would like only items P, Q and R to available from ComboBox2.

    I would also like to load a default value to ComboBox2 depending on the selection made in ComboBox1. For example, if item A is selected from ComboBox1, I would like ComboBox2 to display item X, although X, Y and Z would be available for selection.

    The reason I am adding all items to ComboBox2 in the first instance is because there may be occasions when ComboBox1 s left blank, but a choice is still made from ComboBox2.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Combo box items

    you can use the change event of the first combobox to determine what values to populate in the second combobox. The attached code will give you the solution as described in your example.

    VB Code:
    1. Private Sub ComboBox1_Change()
    2.    
    3.     With Me.ComboBox2
    4.         'First remove existing values from Combo2
    5.         .Clear
    6.         If Me.ComboBox1.Value = "A" Then
    7.             'Add the appropriate items for A
    8.             .AddItem "X"
    9.             .AddItem "Y"
    10.             .AddItem "Z"
    11.             'Select the first item, as an example
    12.             .ListIndex = 0
    13.         ElseIf Me.ComboBox1.Value = "B" Then
    14.             'Add the appropriate items for B
    15.             .AddItem "P"
    16.             .AddItem "Q"
    17.             .AddItem "R"
    18.             'Select the third item, as an example
    19.             .ListIndex = 2
    20.         Else
    21.             'Add all items
    22.             .AddItem "X"
    23.             .AddItem "Y"
    24.             .AddItem "Z"
    25.             .AddItem "P"
    26.             .AddItem "Q"
    27.             .AddItem "R"
    28.             'Select the first item, as an example
    29.             .ListIndex = 0
    30.         End If
    31.     End With
    32. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: [RESOLVED] Combo box items

    Thanks - perfect solution

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width