Introducing the InfoPath SP1 Master/Detail Control

Applies To: Microsoft Office InfoPath 2003 SP-1 Preview

InfoPath SP-1 introduces a number of exciting new built-in controls. One of which is the Master/Detail control. After InfoPath 2003 was released, one of the most common questions on the InfoPath newsgroups was how to implement a Master/Detail type of control in InfoPath. Although this was possible in InfoPath 2003 prior to SP-1, doing so was not easy or straightforward. For this reason, InfoPath 2003 SP-1 now includes built-in support for the Master/Detail control.

The Master/Detail control allows you to designate one control as a master that drives the data in another control which is called the detail. When the user selects an item in the master, the corresponding data in the detail is displayed.

When designing a form template in InfoPath design mode, inserting a Master/Detail control into your form is easy. There are actually two ways to do so. The first way is to use the Master/Detail control in the Controls toolbox. Inserting the Master/Detail control from the Controls toolbox actually inserts two controls – a Repeating Table that is designated as the master and a Repeating Section that is designated as the detail. When you insert the Master/Detail control this way, as long as the “Automatically create data source” checkbox in the Control toolbox is checked, a simple data structure is created in the data source. This structure consists of a group node containing one repeating group node that, in turn, contains four child fields. The master and detail are both bound to the inner repeating group node. In this case, the master and detail are said to be linked by position in the tree. This basically means that if you select the third item in the master control, for example, the data for the third item is displayed in the detail control. This enables you, for example, to build a form that contains basic customer information, such as customer name and SSN, in the master and more detailed information about that customer such as address and phone number, in the detail.

The second way to create a Master/Detail relationship is by manually specifying which control is the master and which is the detail. Although this is a bit more difficult than choosing the Master/Detail control from the Controls toolbox it does give you more control over how the master is linked to the detail. (As mentioned earlier, one way of linking a master control to a detail control is by position. The second way to link a master to a detail is by key field. This type of linking allows you to specify a primary key/foreign key type of relationship where the value of the primary key in the master determines which detail data is displayed. This type of linking is only available when creating a Master/Detail relationship manually.)

To create a Master/Detail relationship manually, perform the following steps:

  1. Insert a Repeating Table control. This control will act as the master. (Only Repeating Tables can act as master controls.)

  2. Open the Repeating Table Properties dialog either by double-clicking on the Repeating Table tab at the bottom of the control or by selecting the Repeating Table and then clicking on the Format | View Properties menu item.

  3. Click on the Master/Detail tab

  4. Click the “Set as master” option button and then type a name into the “Master ID” text box. This ID will be used to specify the master to link to when designating another control as the detail.

  5. Click OK to close the dialog and commit the changes to the Repeating Table properties. (At this point, you will notice that the tab at the bottom of the Repeating Table now contains the text “(master)” which designates that this control is now a master control.)

  6. Now, insert another Repeating Table or a Repeating Section control. This control will act as the detail control. (If you want to link the master to the detail by position, it is better to insert a Repeating Table or Repeating Section control that is bound to the Repeating Table you created in step 1. Link by position is the default behavior when inserting the Master/Detail from the Controls toolbox as mentioned earlier.) If you insert a new Repeating Section control, you will want to add controls inside the Repeating Section in order to display the detail data.

  7. Open the properties dialog for the Repeating Table or Repeating Section as in step 2 above.

  8. Click on the Master/Detail tab

  9. Click the “Set as detail” option button.

  10. From the “Link to Master ID” dropdown, choose the ID that you created in step 4 above.

  11. Now, you have to specify how the master and detail are linked. By default, the master and detail are linked by position. Since we already talked about this type of linking above, click the “By key field” option button instead.

  12. In order for this type of Master/Detail relationship to work, you need to specify which field in the data source is the master key and which field is the detail key.

    1. Click on the data source button to the right of the “Key field (master)” edit box and choose the field from the master that should be used as the master key.

    2. Then, click on the data source button to the right of the “Key field (detail)” edit box and choose the field from the detail that should be used as the detail key.

  13. Click OK to close the properties dialog and commit the changes. (At this point, you will notice that the tab at the bottom of the Repeating Table or Repeating Section now contains the text “(detail)” which designates that this control is now a detail control.)

Now, when you use your form, you will notice that when you select an item in the master, only those detail items whose detail key matches the master key of the currently selected item in the master control will be displayed.

Obviously, if there is no data, no details will be displayed. InfoPath provides a nice way to deal with this situation. When walking through the steps to manually create as Master/Detail relationship above, you may have noticed the option “Copy key field from selected master when inserting detail” which is checked by default. This option designates that when inserting a new detail item, the value of the master key of the item that is currently selected in the master control will be automatically copied to the detail key in the new detail item. This is quite convenient when creating Master/Detail data from scratch.

As you can see, creating a Master/Detail relationship in InfoPath 2003 SP-1 is very easy and is just one of the many new features that were added in direct response to customer feedback.