Freigeben über


Create dependent drop-down / list-box in InfoPath using filters

Summary

Suppose you want to have the items listed in a drop-down be dependent on the selection of another drop-down (this is also known as "cascading dropdowns). If you have InfoPath Service Pack 1 installed, you could use the "Filter Data..." feature to accomplish this.

Example

Picture of two drop downs - state and city.

You have one drop down labeled "State: " with the names of states and another drop-down labeled "City: " which gets populated with the names of the cities for the state you selected on the previous drop down.

The city drop-down obtains its values from an xml file which might look something like this:

<Cities>
<City state="Washington">Redmond</City>
<City state="Washington">Seattle</City>
<City state="California">Los Angeles</City>
  <City state="Florida">Orlando</City>
</Cities>

Implementation

  1. In design mode, right click on the "City" drop down and open the Drop-down Properties dialog.
  2. Select to Look up values in a data connection to a database, Web Service, file, Share Point library or list.
  3. Click Add and follow the wizard to add the xml file containing the city data.
  4. After completing Data Connection Wizard, in the Entries field- select the source for the drop-down values by clicking the button on the right of the field. The Select a Field or Group dialog will appear, for this case you will select the "City" repeating group.
  5. Create a Filter by clicking the Filter Data... button and clicking Add... in the Filter Data dialog.
  6. You will want to create a Filter that displays the data in which the "state" attribute is equal to the field corresponding to the "State: " drop-down (Remember that the field value is kept in the form's "Main" data source)

 Additional Notes/Tips  

  • Previous City selection will persist after changing state. You’ll need to set a Rule to change cities to empty after a change is made to states to avoid selected city remaining in City drop down. (Right click the "State" drop-down in design mode and open the Propeties dialog, click the Rules button and create an Action to set the "City" field value to empty, no condition is necessary since this rule will apply when a change is made).
  • Instead of using drop-down controls you could also use list boxes.
  • You could also use Conditional Formatting to disable the "City:" drop-down until a state is selected. 
  • If you'd like to implement dependent dropdowns in a browser-enabled form template, take a look at this article.

Comments

  • Anonymous
    March 24, 2004
    Is there a way to accomplish this without SP1?

  • Anonymous
    March 24, 2004
    I battled bravely with this for a while before cracking it with a secondary data source I created from an access database but when I tried to hook it up to the SQL server I lost the dependent dropdown values.

    The connection works fine with both dropdowns populated with the full lists until I try to build the filter, then the dependent dropdown drops out.

    Any ideas

    Cheers

    Luigi

  • Anonymous
    March 24, 2004
    From Lauraj's blog, I just read that the Infopath team has a blog. Great news. I didn't realize but then again I haven't been doing much blog related reading (or writing for that matter) for the past couple of weeks so I think it's time to catch up. Today's post is on one of my fave new things from the SP1 preview. Using filters on your dropdown boxes and secondary data sources....

  • Anonymous
    April 13, 2004
    To do this without SP1: lets say you have a query for the data adapter that you want to update based on the selection of another drop-down. What you want to do is write script such that on the change of the primary drop down, you update the query for the adapter you want to refill itself and then requery that adapter.

  • Anonymous
    May 24, 2004
    The comment has been removed

  • Anonymous
    May 25, 2004
    I also have the above problem.

  • Anonymous
    October 15, 2006
    For the two above comments: in InfoPath 2003, you need to employ an XPath current() function, described in this article:http://blogs.msdn.com/infopath/archive/2004/09/13/228881.aspxInfoPath 2007 will do this for you.Thanks,Alex

  • Anonymous
    May 03, 2010
    I have created an InfoPath form and have a specific field linked to another site via a data connection from where I collect a set of alphanumerical codes (ex: WTR001A, GDL542B). I want to semi filter the codes. I don’t want to sort them alphabetically for now, just worry about filtering the options upon selection of a specific building location.My 1st filter field asks the user to select a building location which are:• AMS = Americas• BA = Buenos Aires• BCN = Barcelona• Etc.Upon the user’s selection I want the “codes” mentioned above to display only the ones pertaining to that building location. Perhaps having a XML file that specifies if a user selects Barcelona, then only show BCNXXX “codes” if a user select Singapore, then only SGPXXX codes showWould an XML file work? If so how would it look like?

  • Anonymous
    February 28, 2011
    Thank you man that was a good article

  • Anonymous
    December 06, 2011
    how do I create the xml the city drop down obtain its values from?

  • Anonymous
    December 27, 2012
    I do this 'filter' and it returns only 1 row in the drop down, while it should return 4 or 5 different values in the dropdown.PS: using Infopath 2010.

  • Anonymous
    April 01, 2015
    The comment has been removed

  • Anonymous
    April 09, 2015
    If you are interested in learning advance use of drop down list with Conditional Formattingwww.exceltip.com/.../cascading-drop-down-list-using-conditional-formatting-in-microsoft-excel-2010.html

  • Anonymous
    May 06, 2015
    on the state property, if I add a rule to set City to empty, it does not change at all, the first few selected cities stays there forever.

  • Anonymous
    May 13, 2015
    Any way to get this to work from Excel?

  • Anonymous
    August 17, 2015
    Thank you, Thank you! Solved huge issue for me.