Running code before rules in InfoPath

by S.Y.M. Wong-A-Ton

As you may already know, in InfoPath, rules run before code. Currently InfoPath does not offer any built-in way to run code before rules. But you can use the fact that rules that are defined on a field run whenever the value of that field changes to programmatically set the value of a field in order to run the rules that are defined on that field.

So to run code before rules:

  1. Add a field to your form template and define the rules that you want to run after code has executed.
  2. Add an event handler (either a button, field, or form) to your form template.
  3. In the event handler, write the code that should run prior to running the rules.
  4. In the event handler, after the code of step 3, write code to programmatically set the value of the field on which you defined the rules (step 1) to any valid value. This will trigger the rules on the field to run.

Caveats:
Not all actions that you can define on a button are available on a field. For example, you won’t be able to use a Close the form action in a rule on a field, because this action is not available for fields. The same goes for the Switch views action, which is also only available for buttons.

This trick also works for browser-enabled InfoPath form templates.

Most of what you can do with rules, can be done through code. So do not use this trick if you can write code to obtain the same functionality as a rule would provide.

 Subscribe (RSS | Email)

Related Posts

Comments to this post were closed 30 days after it was published.

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.

Working with InfoPath