Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To create tables that have a master/detail relationship where related detail items are deleted when the master item is deleted, follow these steps:
- Start InfoPath, and then open a new blank form. To do this, follow these steps:
For InfoPath 2007- In the left pane of the Fill Out a Form dialog box, click Design a Form Template.
- In the Design a Form Template window, click Blank, and then click OK.
For InfoPath 2003- In the left pane of the Fill Out a Form dialog box, click Design a Form.
- In the right pane, click New Blank Form.
- Insert two repeating tables. To do this, follow these steps:
For InfoPath 2007- On the Insert menu, click Repeating Table.
- Set the number of columns to 2, and then click OK.
- Repeat step a and step b to insert another repeating table.
For InfoPath 2003- On the Insert menu, click More Controls.
- Under Insert controls in the Controls task pane, click Repeating Table.
- Set the number of columns to 2, and then click OK.
- Repeat step b and step c to insert another repeating table.
- Create two more repeating tables, and then bind the two new tables to the same data as the first two repeating tables. To do this, follow these steps:
- On the View menu, click Data Source.
- In the Data Source task pane, expand group1.
- Right-click group2, and then click Repeating Table.
- In the Data Source task pane, expand group3.
- Right-click group4, and then click Repeating Table.
- Create a master/detail relationship between the two tables that you created in step 3. To do this, follow these steps:
- Right-click the first table, and then click Repeating Table Properties.
Note This table is the third table from the top of the form. - In the Repeating Table Properties dialog box, click the Master/Detail tab.
- Under Master/detail settings, click Set as master.
- In the Master ID box, type Master1, and then click OK.
- Right-click the next table, and then click Repeating Table Properties.
Note This table is the fourth table from the top of the form. - In the Repeating Table Properties dialog box, click the Master/Detail tab.
- Under Master/detail settings, click Set as detail.
- In the Link to master ID box, select Master1.
- Under Link master and detail, click By key field.
- Click the Select XPath icon to the right of the Key field (master) box, and then click field1.
- Click the Select XPath icon to the right of the Key field (detail) box, click field3, and then click OK.
- Insert code to force cascading deletes in the OnAfterChange event for the key field in the details table. To do this, follow these steps:
For InfoPath 2007- In the Data Source task pane, right-click field1 in group2, click Programming, and then click On After Change Event.
- Add the following code to the script-editing window.
Note Replace the field placeholders and the group placeholders with the names of the groups and the fields that you want to use.function msoxd_my_field1::OnAfterChange(eventObj)
{
// Write code here to restore the global state.
if (eventObj.IsUndoRedo)
{
// An undo operation or a redo operation has occurred, and the DOM is read-only.
return;
}
//Delete corresponding details if a delete operation has occurred on source group2.
if(eventObj.Operation == "Delete" && eventObj.Source.nodeName == "my:group2")
{
//Block the view update feature to improve performance.
thisXDocument.View.DisableAutoUpdate();
//Obtain the value of the keyfield of that row that you want to delete (set as field1 in Designer).
var nodeKeyFieldMaster = eventObj.Source.selectSingleNode("my:field1");
//Set the selection on all nodes in Detail.
var nodesDetail = XDocument.DOM.selectNodes("/my:myFields/my:group3/my:group4");
//View each row in Detail, and then delete the row if the key field in the detail matches the key field in the master.
for (i=0; i< nodesDetail.length; i++)
{
//Obtain the row.
var row = nodesDetail.item(i);
//Obtain the value of the keyfield for the current row (set as field3 in Designer).
var nodeKeyFieldDetail = row.selectSingleNode("my:field3");
//Delete the row if the keyfield in the detail matches the keyfield of the deleted row in the master.
if(nodeKeyFieldDetail.text == nodeKeyFieldMaster.text)
{
row.parentNode.removeChild(row);
}
//Re-enable the view update.
thisXDocument.View.EnableAutoUpdate();
}
}
}
- On the File menu, click Save.
- On the File menu, click Exit.
For InfoPath 2003- In the Data Source task pane, right-click field1 in group2, and then click Properties.
- In the Field or Group Properties dialog box, click the Validation and Event Handlers tab.
- In the Events list, click OnAfterChange, and then click Edit.
- Add the following code to the script-editing window.
Note Replace the field placeholders and the group placeholders with the names of the groups and the fields that you want to use.function msoxd_my_field1::OnAfterChange(eventObj)
{
// Write code here to restore the global state.
if (eventObj.IsUndoRedo)
{
// An undo operation or a redo operation has occurred, and the DOM is read-only.
return;
}
//Delete corresponding details if a delete operation has occurred on source group2.
if(eventObj.Operation == "Delete" && eventObj.Source.nodeName == "my:group2")
{
//Block the view update feature to improve performance.
thisXDocument.View.DisableAutoUpdate();
//Obtain the value of the keyfield of that row that you want to delete (set as field1 in Designer).
var nodeKeyFieldMaster = eventObj.Source.selectSingleNode("my:field1");
//Set the selection on all nodes in Detail.
var nodesDetail = XDocument.DOM.selectNodes("/my:myFields/my:group3/my:group4");
//View each row in Detail, and then delete the row if the key field in the detail matches the key field in the master.
for (i=0; i< nodesDetail.length; i++)
{
//Obtain the row.
var row = nodesDetail.item(i);
//Obtain the value of the keyfield for the current row (set as field3 in Designer).
var nodeKeyFieldDetail = row.selectSingleNode("my:field3");
//Delete the row if the keyfield in the detail matches the keyfield of the deleted row in the master.
if(nodeKeyFieldDetail.text == nodeKeyFieldMaster.text)
{
row.parentNode.removeChild(row);
}
//Re-enable the view update.
thisXDocument.View.EnableAutoUpdate();
}
}
}
- On the File menu, click Exit to close the editing window.
- Click OK to close the Field or Group Properties dialog box.
Preview the form to demonstrate how the code works.To do this, follow these steps:
- For InfoPath 2007
On the File menu, point to Preview, and then click Form.
For InfoPath 2003
On the File menu, point to Preview Form, and then click Default - Insert some test values into the first table on the form. This table is the Master table. To do this, follow these steps:
- In the first column of the first row, type Test1.
- In the second column of the first row, type Master1.
- Under the row where you just typed the data, click Insert item to add a new row to the first table.
- In the first column of the second row, type Test2.
- In the second column of the second row, type Master2.
- If you want to add more rows, repeat steps 2c through 2e.
- Insert some test values into the second table on the form. This table is the Details table. To do this, follow these steps:
- In the first column of the first row, type Test1.
- In the second column of the first row, type Detail1.
- Under the row where you just typed the data, click Insert item to add a new row to the second table.
- In the first column of the second row, type Test2.
- In the second column of the second row, type Detail2.
- If you want to add more rows, repeat steps 2c through 2e.
- Test the Master/Detail tables.
Select each row in the third table, one at a time.
The fourth table shows only the rows from the second table that have the same value in the first column as the selected row in the third table.
- Delete a row from the Master table.
To do this, follow these steps:
- In the third table, select the second row where the first column contains "Test2".
- Click the arrow next to the selected row, and then click Remove group2.
Note The rows that have the same text in the first column are automatically deleted from the fourth table by the sample code. For example, "Test2" is deleted.