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.

ADVERTISEMENTS

Introduction to filling InfoPath form fields when the form opens

There are two ways to fill an InfoPath form when it is opened:

  1. Use rules
  2. 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

To use rules to load data when an InfoPath forms opens:

  1. On the Tools menu, click Form Options.
  2. On the Form Options dialog box, click Open and Save under Category, and then click Rules.
  3. On the Rules for Opening Forms dialog box, click Add.
  4. On the Rule dialog box, click Add Action.
  5. On the Action dialog box, select Set a field's value from the Action drop-down list box.
  6. 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.
  7. 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.
  8. 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:

  1. On the Tools menu, select Programming, and then click Loading Event.
  2. 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).

 
 Subscribe for updates via RSS or Email

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.

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 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

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