Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

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

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

  • Comments 11

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.
Leave a Comment
  • Please add 1 and 7 and type the answer here:
  • Post
  • Is there a way to accomplish this without SP1?
  • 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
  • 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....
  • 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.
  • I have a dependent drop-downs/list-box in InfoPath using Filters working in a repeating table for the first row; however, the subsequent rows contain any previously filtered data.

    I am sure there is a simple way to fix it. I am just over looking it.
  • I also have the above problem.
  • 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.aspx

    InfoPath 2007 will do this for you.

    Thanks,

    - Alex

  • 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 show

    Would an XML file work? If so how would it look like?

  • Thank you man that was a good article

  • how do I create the xml the city drop down obtain its values from?

  • 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.

Page 1 of 1 (11 items)