InfoPath Basics: How to populate an InfoPath form when it is opened/loaded
This article explains how to use rules or code to fill form fields when an InfoPath form is opened or loaded.
Introduction to filling InfoPath form fields when the form opens
There are two ways to fill an InfoPath form when it is opened:
- Use rules
- Write code
When do you use rules and when do you use code? A rule of thumb that I'd recommend applying is: Only write code if the functionality you require cannot be achieved using rules.
For example, if you want to set the value of a Text Box, you can use rules to do this, but if you want to add rows to a Repeating Table, you will have to write code to do this, because InfoPath does not support adding rows to a Repeating Table using rules.
You can do a lot in InfoPath without writing a single line of code. One of the primary design goals of InfoPath is to save you time by not having to write code. So whenever possible, try to take advantage of this. I've written a good few InfoPath tutorials that do not require code to help you get started.
How to use rules to fill form fields when an InfoPath form loads
- On the Tools menu, click Form Options.
- On the Form Options dialog box, click Open and Save under Category, and then click Rules.
- On the Rules for Opening Forms dialog box, click Add.
- On the Rule dialog box, click Add Action.
- On the Action dialog box, select Set a field's value from the Action drop-down list box.
- On the Action dialog box, click the button behind the Field text box and select the field for which you want to set a value.
- On the Action dialog box, click the button behind the Value text box and enter a formula or select a field for the value of the field you want to set.
- Close all open dialog boxes by clicking OK.
How to use code to fill form fields when an InfoPath form loads
To use code to load data when an InfoPath form opens:
- On the Tools menu, select Programming, and then click Loading Event.
- Write code in the Loading event to set the values of chosen InfoPath form fields.
For code on how to programmatically set the value of a field on an InfoPath form, see How do I set the value of an InfoPath field through code? in the InfoPath 2007 Frequently Asked Questions (FAQ).
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:
- InfoPath Basics: Understanding Rules in InfoPath
- Programmatically add items to a drop-down list box in InfoPath 2007
- Auto-fill a repeating table with data from a SharePoint list when a row is inserted
- InfoPath 2010 Tutorial: What is InfoPath 2010 form load?
- InfoPath 2010 Demo: Show / hide section on time of day