How to extract the month name from a date picker in InfoPath

Learn how to use a secondary data source, rules, filters, and InfoPath functions to extract the month name from the secondary data source based on the date that is displayed in a date picker control.

ADVERTISEMENTS

Problem

You have an InfoPath date picker control on a form. You would like to select a date from the date picker control and display the name of the month that corresponds to the selected date in a text box.

Solution

Use an XML file that contains the month names, create a secondary data connection to this XML file, and then use Rules and Filters together with the number() and substring() InfoPath functions to extract the month name based on the date selected in a date picker.

Discussion

You can achieve this functionality as follows:

  1. Create an XML file named monthnames.xml that has the following contents:

    <?xml version="1.0" encoding="UTF-8"?>
    <months>
    <month id="1" name="January"/>
    <month id="2" name="February"/>
    <month id="3" name="March"/>
    <month id="4" name="April"/>
    <month id="5" name="May"/>
    <month id="6" name="June"/>
    <month id="7" name="July"/>
    <month id="8" name="August"/>
    <month id="9" name="September"/>
    <month id="10" name="October"/>
    <month id="11" name="November"/>
    <month id="12" name="December"/>
    </months>
  2. In InfoPath, create a new Blank form template.
  3. On the Tools menu, click Data Connections.
  4. On the Data Connections dialog box, click Add, and follow the instructions on the Data Connection Wizard to add a Receive data connection to the XML document you created in step 1.
  5. Add a Date Picker control and a Text Box control to the InfoPath form template.
  6. Double-click the Date Picker control to open its Properties dialog box.
  7. On the Date Picker Properties dialog box, click Rules.
  8. On the Rules dialog box, click Add.
  9. On the Rule dialog box, click Add Action.
  10. On the Action dialog box, leave Set a field's value selected in the Action drop-down list box.
  11. On the Action dialog box, click the button behind the Field text box.
  12. On the Select a Field or Group dialog box, select the field that represents the Text Box control on the InfoPath form template, and click OK.
  13. On the Action dialog box, click the button behind the Value text box.
  14. On the Insert Formula dialog box, click Insert Field or Group.
  15. On the Select a Field or Group dialog box, select monthnames from the Data source drop-down list box, expand all of the nodes, select the name node, and click Filter Data.
  16. On the Filter Data dialog box, click Add.
  17. On the Specify Filter Conditions dialog box, select id from the first drop-down list box, select is equal to from the second drop-down list box, and select Use a formula from the third drop-down list box.
  18. On the Insert Formula dialog box, type:

    number(substring(
  19. On the Insert Formula dialog box, click Insert Field or Group.
  20. On the Select a Field or Group dialog box, select Main from the Data source drop-down list box, select the field that represents the Date Picker control on the InfoPath form template, and click OK.
  21. On the Insert Formula dialog box, type:

    , 6, 2))

    The complete formula should look something like:

    number(substring(field1, 6, 2))

    where field1 represents the Date Picker control on the InfoPath form template.

  22. Click OK on all open dialog boxes.

You should now have a fully functional form so that when you select a date in the date picker, the corresponding month name will be displayed in the text box.

 
 Subscribe for updates via RSS or Email

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services