Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to automatically display the first item in a drop-down list box in InfoPath 2003


Author: S.Y.M. Wong-A-Ton MVP

View products that this article applies to.

Summary

This article describes how to automatically display the first item in a drop-down list box that has been populated with values from a data connection to an XML file.

↑ Back to the top


More information


In order�to display the first item as the selected item in�a drop-down list box, you must set the value of the drop-down list box equal�to the value of the first item in the list that populates the drop-down list box. This article uses an XML file to populate a drop-down list box on an InfoPath form�and a rule, which�uses the position() XPath function, to find the value of the first item in the XML file. This value is then used to set the value of the drop-down list box to display the first item as the selected item.

Create an XML file

  1. Open Notepad.
  2. Copy the following XML and paste it into Notepad.
    <?xml version="1.0" encoding="UTF-8"?>
    <items>
    � <item>
    ��� <value>MDB</value>
    ��� <displayName>Access</displayName>
    � </item>
    � <item>
    ��� <value>XLS</value>
    ��� <displayName>Excel</displayName>
    � </item>
    � <item>
    ��� <value>XSN</value>
    ��� <displayName>InfoPath</displayName>
    � </item>
    � <item>
    ��� <value>PPT</value>
    ��� <displayName>PowerPoint</displayName>
    � </item>
    � <item>
    ��� <value>DOC</value>
    ��� <displayName>Word</displayName>
    � </item>
    </items>
  3. On the File menu, click Save As.
  4. In the Save As dialog box, click�Desktop.
  5. In�the Save As dialog box,�type�items.xml in the File name�text box.
  6. In the Save As dialog box, select All Files from the Save as type�drop-down list box.�
  7. In the Save As dialog box, click Save.

Design�an InfoPath form

  1. Start InfoPath. On the File menu, click Design a Form.
  2. In the Design a Form task pane, click New Blank Form.
  3. In the Design Tasks task pane, click Controls.
  4. In the Insert Controls list, click Drop-Down List Box.
    A new drop-down list box�that is named field1 is added to the form.
  5. In Microsoft InfoPath Designer, right-click field1, and then�select Drop-Down List Box Properties.
  6. In the Drop-Down List Properties dialog box, select Look up values in a data connection to a database, Web service, file, or SharePoint library or list, and then click Add.
  7. In the Data Connection Wizard, leave XML document selected, and click Next.
  8. In the Data Connection Wizard, click�Browse.
  9. In the Open dialog box, click�Desktop.
  10. In the Open dialog box, click�items.xml, and then click Open.
  11. In the Data Connection Wizard, click Next,�and then�Finish.
  12. Choose Yes when you are prompted to add the XML file to your form so that it will be accessible from other computers.
  13. In the Drop-Down List Box Properties dialog box, click the Select XPath button behind the Entries text box.
  14. In the Select a Field or Group dialog box, click the�item node, and then�click OK.
  15. In the Drop-Down List Box Properties dialog box, click the Select XPath button behind the Display name text box.
  16. In the Select a Field or Group dialog box, click the�displayName node,and then�click OK.
  17. In the Drop-Down List Box Properties dialog box, click OK.

Create a rule

  1. On the Tools menu, click Form Options.
  2. In the Form Options dialog box, click the Open and Save tab, and then click Rules.
  3. In the Rules for Opening Forms dialog box, click Add.
  4. In the Rule dialog box, click Add Action.
  5. In the Action dialog box, select Set a field's value from the Action drop-down list box.
  6. In the Action dialog box, click the button behind the Field text box.
  7. In the Select a Field or Group dialog box, click field1, and then click OK.
  8. In the Action dialog box, click the button behind the Value text box.
  9. In the Insert Formula dialog box, click Insert Field or Group.
  10. In the Select a Field or Group dialog box, select items (Secondary) from the Data source drop-down list box.
  11. In the Select a Field or Group dialog box, expand the item node, click the value node, and then click Filter Data.
  12. In the Filter Data dialog box, click Add.
  13. In the Specify Filter Conditions dialog box, select The expression from the first drop-down list box.
  14. In the Specify Filter Conditions dialog box, type the following expression in the text box:
    position() = 1
  15. In the Specify Filter Conditions dialog box, click OK.
  16. In the Filter Data dialog box, click OK.
  17. In the Select a Field or Group dialog box, click OK.
  18. In the Insert Formula dialog box, click OK.
  19. In the Action dialog box, click OK.
  20. In the Rule dialog box, click OK.
  21. In the Rules for Opening Forms dialog box, click OK.
  22. In the Form Options dialog box, click OK.

Test�the InfoPath�form

  1. On the toolbar, click Preview Form.
  2. Notice that the�drop-down list box�automatically�displays the first item in the list�as soon as�the InfoPath form�opens.

The technique described�in this article�also works�for drop-down list boxes that�are�populated with values�from�the form's data source or from a data connection to a database, Web service, or SharePoint library or list.

↑ Back to the top


Community solutions content disclaimer

Microsoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.

↑ Back to the top


Keywords: kbpubmvp, kbpubtypecca, kbhowto, KB555838

↑ Back to the top

Article Info
Article ID : 555838
Revision : 1
Created on : 12/16/2006
Published on : 12/16/2006
Exists online : False
Views : 396