Create a day of the week InfoPath form using rules and formulas

Use a formula to calculate the number for the day of the week (Monday through Sunday) based on a date specified in a Date Picker control, and conditional formatting on sections to show/hide the weekday sections on an InfoPath form.

ADVERTISEMENTS

Problem

I've previously written about how you can use JScript and xdExtensions in InfoPath 2003 to retrieve the weekday and switch between "day of the week" sections on an InfoPath form.

I came up with that solution to solve the following problem: You have a TODO list in InfoPath for each weekday (similar to a daily agenda) and would like to be able to display the list for a day of the week (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday) which is determined by a chosen date.

As I was rewriting this solution to work in InfoPath 2007, I discovered that you don't really have to write code - if you don't want to - to get this functionality.

Solution

Use a Rule and Formula to determine which weekday corresponds to the selected date and use Conditional Formatting in InfoPath to show/hide the section that belongs to a particular day of the week.

Discussion

You can accomplish this functionality as follows:

  1. Create a New Blank Form in InfoPath.
  2. Add a Table with Title to the InfoPath form.
  3. Add a Date Picker control to the table and rename it to selectedDate.
  4. Add 7 Section controls below the Date Picker control.
  5. On the Data source task pane, add a text field named dayOfWeek under the myFields node. Your Main data source should now resemble the following figure:

    Main data source of the InfoPath form template to calculate the day of the week
    Figure 1. Main data source of the InfoPath form template to calculate the day of the week.
  6. Type the text Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday on each subsequent Section control.
  7. Modify the table and controls to resemble the following figure:
    The InfoPath form template to switch between weekdays in Design mode
    Figure 2. The InfoPath form template to switch between weekdays in Design mode.
  8. Double-click the Date Picker control to open its Properties dialog box.
  9. On the Date Picker Properties dialog box, click the Insert Formula button behind the Default Value text box.
  10. On the Insert Formula dialog box, click Insert Function, select today from the list of functions, and click OK.
  11. On the Insert Formula dialog box, click OK.
  12. On the Date Picker Properties dialog box, click Rules.
  13. On the Rules dialog box, click Add.
  14. On the Rule dialog box, click Add Action.
  15. On the Action dialog box, select Set a field's value from the drop-down list box, select the dayOfWeek field as the Field to set, and click the formula button behind the Value text box.
  16. On the Insert Formula dialog box, select the Edit XPath (advanced) check box, and copy/paste the following formula into the Formula text box:

    (number(substring(., 9, 2)) + number(number(substring(., 1, 4)) - (floor((14 - number(substring(., 6, 2))) div 12))) + floor(number(number(substring(., 1, 4)) - (floor((14 - number(substring(., 6, 2))) div 12))) div 4) - floor(number(number(substring(., 1, 4)) - (floor((14 - number(substring(., 6, 2))) div 12))) div 100) + floor(number(number(substring(., 1, 4)) - (floor((14 - number(substring(., 6, 2))) div 12))) div 400) + floor((31 * number(number(substring(., 6, 2)) + 12 * (floor((14 - number(substring(., 6, 2))) div 12)) - 2)) div 12)) mod 7

  17. Click OK on all open dialog boxes.
  18. Double-click the first Section control.
  19. On the Section Properties dialog box, click the Display tab, and then click Conditional Formatting.
  20. On the Conditional Formatting dialog box, click Add.
  21. On the Conditional Format dialog box, add a condition that says:

    dayOfWeek is not equal to 1

  22. On the Conditional Format dialog box, under Then apply this formatting: check the Hide this control check box.
  23. Click OK on all open dialog boxes.
  24. Repeat steps 18 through 23 for the rest of the Section controls, using the following conditions:

    dayOfWeek is not equal to 2

    for the Tuesday section,

    dayOfWeek is not equal to 3

    for the Wednesday section,

    dayOfWeek is not equal to 4

    for the Thursday section,

    dayOfWeek is not equal to 5

    for the Friday section,

    dayOfWeek is not equal to 6

    for the Saturday section,

    dayOfWeek is not equal to 0

    for the Sunday section.

You should now have a fully functional InfoPath form so that when you select a date from the date picker control, the corresponding section for the day of the week is displayed.

 
 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