InfoPath Basics: Understanding Rules in InfoPath
This article explains what Rules are in InfoPath and how you can use them.
Introduction to Rules
A Rule in InfoPath is a set of one or more Actions, which are executed when an event takes place. An event can be seen as a trigger for a Rule.
Events in InfoPath can take place when you:
- Open an InfoPath form. For an example, see InfoPath Basics: How to populate an InfoPath form when it is opened/loaded.
- Click a button.
- Change the value of a field.
- Insert a row or section in a Repeating Table or a Repeating Section. For an example, see Auto-fill a repeating table with data from a SharePoint list when a row is inserted.
- Submit an InfoPath form.
How to add a Rule
You can use the Rules dialog box to add a rule to a control on an InfoPath form. You can access this dialog box by clicking on the Rules button on the Properties dialog box of the control to which you want to add the rule.
Figure 1. The Rules dialog box in InfoPath.
You can open the Properties dialog box of a control on one of 3 ways:
- Double-click the control.
- Click the control to select it and then simultaneously press the Alt and Enter keys on your keyboard.
- Click the control to select it and then on the Format menu, click the Properties menu item for the control.
Choosing the appropriate control to add a rule to
Because Rules always run after an event has taken place, you must always add a rule to the control that will generate the event.
For example, if you want to fill a Text Box control with a piece of text when you select an item from a Drop-Down List Box control (see Auto-fill fields in InfoPath from a drop-down list box selection and secondary data source) then you must add the rule to the Drop-Down List Box control, because the Drop-Down List Box will raise a Change Event after an item has been selected from it.
Setting a Condition on a Rule
If you want a Rule to run only if a specified condition is met, you can add a Condition to the Rule to restrict the Rule to run only if that condition has been met.
For example, going back to the previous example of filling a Text Box when an item is selected from a Drop-Down List Box, if you want to only fill the Text Box (fieldA) if another Text Box (fieldB) on the same InfoPath form contains a value, you could add a Condition on the Rule for the Drop-Down List Box.
This Condition should ensure that the other Text Box (fieldB) on the InfoPath form contains a value (is not blank), so that the Rule for setting the value of the Text Box (fieldA) after an item is selected from the Drop-Down List Box, only runs if fieldB already contains a value.
Figure 2. The Condtions dialog box in InfoPath.
To add such a Condition to the Rule:
- On the Rule dialog box, click Set Condition.
- On the Condition dialog box, select fieldB from the first drop-down list box.
- On the Condition dialog box, select is not blank from the second drop-down list box.
- Click OK.
Before you set this Condition on the Rule, the InfoPath form would behave as follows:
- If fieldB does or does not contain a value and you select an item from the drop-down list box, the value of fieldA will be set to whatever value you have defined through the Rule.
After you set this Condition on the Rule, the InfoPath form will behave as follows:
- If fieldB contains a value and you select an item from the drop-down list box, the value of fieldA will be set to whatever value you defined through the Rule.
- If fieldB does not contain a value and you select an item from the drop-down list box, fieldA will not change and/or will remain empty.
InfoPath was built to save you time. Rules and Conditions form a major component of this time-saving strategy. You can do a lot in InfoPath without writing a single line of code by using Rules. If you learn how to creatively apply Rules, you can avoid writing code in many cases.
For more examples of how to use Rules and Conditions in InfoPath, see Codeless InfoPath Tutorials, Articles, and Solutions. For an in-depth coverage of adding and working with rules in InfoPath 2010, see Chapter 4 in InfoPath 2010 Cookbook; and Chapter 4 in InfoPath 2013 Cookbook if you are using InfoPath 2013. Both books contain many examples of the usage of rules, since both books teach you how to get things done with InfoPath without writing code.
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.
Related InfoPath Articles:
- Simulate Excel's VLOOKUP function in InfoPath to look up data in a secondary data source using rules
- Auto-fill a repeating table with data from a SharePoint list when a row is inserted
- Calculate the difference between two date picker controls in InfoPath using rules and formulas
- Calculating time differences in InfoPath without custom code
- Find rules with the Rule Inspector in InfoPath