none
Userform with combobox2 loaded based upon selection in combobox1 RRS feed

  • Question

  • Word 2000:

    I would like to have a userform with 2 comboboxes, 1st one loaded with data from database and the 2nd loaded with info from database based upon selection in combobox1. 

    How can I load combo2 based upon selection of combo1?

    I could do this with 2 userforms but wnat to have on one.

    How do I code for this?


    Bryan Sorenson
    Wednesday, May 25, 2011 7:02 PM

Answers

  • Hi Bryan,

    There is a ComboBox1.Clear command and also a ComboBox1.RemoveItem(index) command.


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by sorney Monday, May 30, 2011 9:24 PM
    Monday, May 30, 2011 4:38 PM

All replies

  • Hi Bryan,

    I think something like this VBA should work.

    Private Sub CommandButton1_Click()
        If ComboBox1.ListCount >= 1 Then
            If ComboBox1.ListIndex = -1 Then
                MsgBox "You did not select an item.", vbCritical + vbOKOnly, "Your Application Name"
                Exit Sub
            End If
        Else
            MsgBox "There is nothing to select for adding.", vbCritical + vbOKOnly, "Your Application Name"
        End If
        If ComboBox1.Value = "Your Value" Then
            ComboBox2.AddItem ("a value from your DB")
        End If
    End Sub

    You could also put similar code in this combo box routine.

    Private Sub ComboBox1_Change()

    End Sub


    Kind Regards, Rich ... http://greatcirclelearning.com
    Thursday, May 26, 2011 4:44 AM
  • Hi Rich,

    Can you expand a bit on the combobox1_change? What triggers this and how are values retireved from combo1 in order to sql another table to load combobox2?

    In loading combo1 I will be getting 2 fields from Table1, call 'A' and 'B'. 'A' being a description and displayed, 'B' being the code and not displayed. This part I got.

    So within combobox1_change I would sql database Table2 where the code from Table1 equals the code from Table2, correct?

    The Click event would then would retrieve values from combo1 and combo2 I would assume?

    Thanks for your insight,

    Bryan


    Bryan Sorenson
    Thursday, May 26, 2011 11:57 AM
  • Hi Bryan,

    ComboBox1_Change is triggered by the user making a change to one of the listed items in the combobox. Based on what I now understand of your need the ComboBox1_Click event would be more appropriate. The value of the item clicked is obtained from the ComboBox1.Value command.

    Private Sub ComboBox1_Click()

    Answering your other question, "So within combobox1_change I would sql database Table2 where the code from Table1 equals the code from Table2, correct?" the answer is Yes, except you would be using the ComboBox1_Click event and then based on what was selected from ComboBox1 you'd find the matching value from your Table 2 sql database.

    Hope this helps

     

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Thursday, May 26, 2011 1:48 PM
  • Hi Rich,

    I think I understand this and will work on early next week.

    What invlokes the ComboBox1_Click() , selecting a value from the drop down?

    The CommandButton1_Click then would give me the values selected in combobox1 and combobox2, correct?

     

    Again thanks much for the help.

    Bryan


    Bryan Sorenson
    Thursday, May 26, 2011 4:03 PM
  • Bryan,

    The ComboBox1_Click is invoked by the user clicking on one of the items and you have that value immediately. If your code then reacts to the ComboBox1_Click event and picks up the necessary data from your Table 2, it could then insert it directly into your ComboBox2.

    The CommandButton1_Click event is only executed if your form contains a Command Button and you want to do some action based on it's click.


    Kind Regards, Rich ... http://greatcirclelearning.com
    Thursday, May 26, 2011 5:53 PM
  • Rich,

    Thanks much for all your insight on this. I will work on this over the weekend.


    Bryan Sorenson
    Friday, May 27, 2011 11:39 AM
  • See http://gregmaxey.mvps.org/Populate_UserForm_ListBox.htm


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "sorney" wrote in message news:6c3575f2-8c10-4a3a-aab0-9f904656f7fc@communitybridge.codeplex.com...

    Word 2000:

    I would like to have a userform with 2 comboboxes, 1st one loaded with data from database and the 2nd loaded with info from database based upon selection in combobox1.

    How can I load combo2 based upon selection of combo1?

    I could do this with 2 userforms but wnat to have on one.

    How do I code for this?


    Bryan Sorenson


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Saturday, May 28, 2011 5:39 AM
  • Hi Rich,

    Have been working on this and have 1 question.

    How do I clear the contents of combobox2 when I make a different selection in combobox1?

    What is happening is that when I select a value from combo1 I get the appropriate values for combo2 but, when I then select a different value in combo1 I get the values from the previous and then the values for the second selection.

     

    Thanks,

    Bryan


    Bryan Sorenson
    Monday, May 30, 2011 3:34 PM
  • Hi Bryan,

    There is a ComboBox1.Clear command and also a ComboBox1.RemoveItem(index) command.


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by sorney Monday, May 30, 2011 9:24 PM
    Monday, May 30, 2011 4:38 PM
  • Perfect Rich!

    Used combobox2.clear at the start of combobox1_click()

    I would assume that one would need to loop in order to clear contents if one were to use the RemoveItem(index) command, correct?

     


    Bryan Sorenson
    Monday, May 30, 2011 7:50 PM
  • Hi Bryan,

    You could loop but mostly that command is for selective deletes. For example if you wanted to allow the user to delete certain items from a list. When a selective item is deleted then the items below it move up on the list automatically.


    Kind Regards, Rich ... http://greatcirclelearning.com
    Monday, May 30, 2011 8:52 PM