InfoPath Basics: Understanding Rules in InfoPath

Applies to: InfoPath 2007

This article explains what Rules are in InfoPath and how you can use them.

ADVERTISEMENTS

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:

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.

Rules dialog box in InfoPath

Figure 1. The Rules dialog box in InfoPath.

You can open the Properties dialog box of a control on one of 3 ways:

  1. Double-click the control.
  2. Click the control to select it and then simultaneously press the Alt and Enter keys on your keyboard.
  3. 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.

Conditions dialog box in InfoPath

Figure 2. The Condtions dialog box in InfoPath.

To add such a Condition to the Rule:

  1. On the Rule dialog box, click Set Condition.
  2. On the Condition dialog box, select fieldB from the first drop-down list box.
  3. On the Condition dialog box, select is not blank from the second drop-down list box.
  4. 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.

Conclusion

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.

 


Related InfoPath Articles:

 

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. 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.

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