Forum Discussion

DCat1223's avatar
DCat1223
Frequent Contributor
9 years ago

Filter UserForm ComboBox based on previous ComboBox selection

Hi…

I have question regarding the use of ComboBoxes on a UserForm in a Keyword test. I have a “Department” ComboBox that is populated with the following…  “Dariy”, “Frozen”, “Grocery”…etc.  I would like to have a second ComboBox that displays the warehouses associated with the departments – basically the “Warehouse” ComboBox is pre-filtered based on the department selected, thus limiting the User’s Warehouse selection to only the warehouses that service the previously selected department.

 

If user selects department “Dairy”, the Warehouse ComboBox displays: “Warehouse 100”, “Warehouse 200”

If user selects department “Frozen”, the Warehouse ComboBox displays “Warehouse120”, “Warehouse 220”

If user selects department “Grocery”, the Warehouse ComboBox displays “Warehouse 110”, “Warehouse 210”.

 

I think you get the idea.

 

Is this even possible in a keyword test using the Items property (or any other property for that matter) of the ComboBoxes?

 

Thank you in advance for your time!

 

Dave.

  • Add the Clear method at the beginning of the OnChange event handler to clear the list of Warehouse combobox. Here is my code:

    function UserForm1_cxComboBox2_OnChange(Sender)
    {
      UserForms.UserForm1.cxComboBox1.Properties.Items.Clear();
      if (UserForms.UserForm1.cxComboBox2.Text == "Frozen")
       {   
          with (UserForms.UserForm1.cxComboBox1.Properties.Items)
        {
          Add("100-Warehouse 100");
          Add("200-Warehouse 200");
        }
        }
      if (UserForms.UserForm1.cxComboBox2.Text == "Dairy")
       {   
          with (UserForms.UserForm1.cxComboBox1.Properties.Items)
        {
          Add("110-Warehouse 110");
          Add("210-Warehouse 210");
        }
        }
    }
    

    Does it help?

     

    • DCat1223's avatar
      DCat1223
      Frequent Contributor

      Thank you, Tanya.  This looks promising. 

      I am getting an error and can't seem to triage this.  Please see attached screenshot.

      What am I doing wrong here?

       

       

       

       

      • TanyaYatskovska's avatar
        TanyaYatskovska
        SmartBear Alumni (Retired)

        Hi,

         

        You need to call Add inside with. The with operator allows you to define an object where Add will belong. Take a look at your script where you fill in the combobox based on Dairy.