IF-ELSE statements as conditions on rules in InfoPath 2010
Learn how to convert if-else statements into conditions on action rules in InfoPath 2010.
A condition on a rule in InfoPath determines when a rule should run.
If you are familiar with if-else statements in programming languages, you may want to know how to convert such statements into conditions on rules in InfoPath 2010.
Suppose you have two text boxes (field1 and field2) and a button on an InfoPath form and want the value of field2 to be set based on the value of field1 when the button is clicked.
If you want to convert the following if-else statement
if (field1 == "1")
field2 = "one"
else
field2 = "none"
into conditions on rules, you would have to:
- Add an Action rule to the button with a Condition that says:
field1 = "1"
and an action that says:
Set a field's value: field2 = "one"
and select the Don't run remaining rules if the condition of this rule is met check box. This rule defines the first part of the if-else statement and will stop all other rules from running if the condition is met. Rules run from top to bottom in InfoPath 2010, so if you do not stop other rules from running if the condition is met, any other rules that come after this rule might overwrite the results of the action from this rule. - Add a second Action rule to the button with an action that says:
Set a field's value: field2 = "none"
This rule does not require a condition, because it will only run if the condition on the first rule is not met.
If you want to convert the following if-else statement
if (field1 == "1")
field2 = "one"
else if (field1 == "2")
field2 = "two"
else
field2 = "none"
into conditions on rules, you would have to create 3 action rules, add conditions to the first two rules and stop all rules from running if any of the conditions is met.
So:
- Add an Action rule to the button with a Condition that says:
field1 = "1"
and an action that says:
Set a field's value: field2 = "one"
and select the Don't run remaining rules if the condition of this rule is met check box. - Add a second Action rule to the button with a Condition that says:
field1 = "2"
and an action that says:
Set a field's value: field2 = "two"
and select the Don't run remaining rules if the condition of this rule is met check box. - Add a third Action rule to the button with an action that says:
Set a field's value: field2 = "none"
Related InfoPath Articles:
- InfoPath Basics: Understanding Rules in InfoPath
- InfoPath Basics: Make a control required based on a condition
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. Usage of 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.



