Calculating a person's age given a date of birth using rules and formulas in InfoPath

Use rules, conditions, the today() function, the number() function, and the substring() function in InfoPath to calculate a person's age from his/her date of birth.

ADVERTISEMENTS

Problem

You want to calculate a person's age given his/her date of birth in InfoPath, but you do not want to write code to do this.

Solution

Use rules, conditions, the today() function, the number() function, and the substring() function in InfoPath to calculate a person's age given his/her date of birth.

Discussion

You can accomplish this functionality as follows:

  1. Create a New Blank Form in InfoPath.
  2. Add a Date Picker control to the form template and rename it to dob.
  3. Add a Text Box control to the form template and rename it to age. Change its data type to Whole Number (integer) and make it Read-only.
  4. Add 3 rules to the dob field:
    1. The Action on the first Rule must say:

      Set a field's value: age = number(substring(today(), 1, 4)) - number(substring(., 1, 4))
    2. The Action on the second Rule must say:

      Set a field's value: age = number(substring(today(), 1, 4)) - number(substring(., 1, 4)) - 1

      Add the following 3 Conditions to this rule:

      number(substring(xdDate:Today(), 9, 2)) < number(substring(../my:dob, 9, 2))

      and

      number(substring(xdDate:Today(), 6, 2)) = number(substring(../my:dob, 6, 2))

      or

      number(substring(xdDate:Today(), 6, 2)) < number(substring(../my:dob, 6, 2))

      Note: You will have to select The expression from the first dropdown list box in the Condition dialog box to be able to enter the expressions above.

    3. The Action on the third Rule must say:

      Set a field's value: age = 0

      Add a Condition to this rule that says:

      age < 0

      To add the last condition, on the Condition dialog box, select age from the first drop-down list box, is less than from the second drop-down list box, and type in the number 0 in the text box.

The age field should now be populated with an amount of years when you fill out a date of birth in the InfoPath form.

 
 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