Microsoft KB Archive/892952

= How to use script for cascading deletes in tables that have a master/detail relationship in InfoPath =

Article ID: 892952

Article Last Modified on 1/30/2007

-

APPLIES TO


 * Microsoft Office InfoPath 2003
 * Microsoft Office InfoPath 2003, Service Pack 1 (SP1)
 * Microsoft Office InfoPath 2007

-





INTRODUCTION
This article describes how to use script for cascading deletions in tables that have a master/detail relationship in Microsoft Office InfoPath. You can use this code to delete detail items that are linked to a master item by using a key field when you delete the master item.



MORE INFORMATION
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.</ol> </li> Insert two repeating tables. To do this, follow these steps:

For InfoPath 2007  On the Insert menu, click Repeating Table.</li> Set the number of columns to 2, and then click OK.</li> Repeat step a and step b to insert another repeating table.</li></ol>

For InfoPath 2003  On the Insert menu, click More Controls.</li> Under Insert controls in the Controls task pane, click Repeating Table.</li> Set the number of columns to 2, and then click OK.</li> Repeat step b and step c to insert another repeating table.</li></ol> </li> 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.</li> In the Data Source task pane, expand group1.</li> Right-click group2, and then click Repeating Table.</li> In the Data Source task pane, expand group3.</li> <li>Right-click group4, and then click Repeating Table.</li></ol> </li> <li>Create a master/detail relationship between the two tables that you created in step 3. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Right-click the first table, and then click Repeating Table Properties.

Note This table is the third table from the top of the form.</li> <li>In the Repeating Table Properties dialog box, click the Master/Detail tab.</li> <li>Under Master/detail settings, click Set as master.</li> <li>In the Master ID box, type Master1, and then click OK.</li> <li>Right-click the next table, and then click Repeating Table Properties.

Note This table is the fourth table from the top of the form.</li> <li>In the Repeating Table Properties dialog box, click the Master/Detail tab.</li> <li>Under Master/detail settings, click Set as detail.</li> <li>In the Link to master ID box, select Master1.</li> <li>Under Link master and detail, click By key field.</li> <li>Click the Select XPath icon to the right of the Key field (master) box, and then click field1.</li> <li>Click the Select XPath icon to the right of the Key field (detail) box, click field3, and then click OK.</li></ol> </li> <li>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 <ol style="list-style-type: lower-alpha;"> <li>In the Data Source task pane, right-click field1 in group2, click Programming, and then click On After Change Event.</li> <li> 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 == &quot;Delete&quot; && eventObj.Source.nodeName == &quot;my:group2&quot;) { //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(&quot;my:field1&quot;); //Set the selection on all nodes in Detail. var nodesDetail = XDocument.DOM.selectNodes(&quot;/my:myFields/my:group3/my:group4&quot;); //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(&quot;my:field3&quot;); //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;

} } } </li> <li>On the File menu, click Save.</li> <li>On the File menu, click Exit.</li></ol>

For InfoPath 2003 <ol style="list-style-type: lower-alpha;"> <li>In the Data Source task pane, right-click field1 in group2, and then click Properties.</li> <li>In the Field or Group Properties dialog box, click the Validation and Event Handlers tab.</li> <li>In the Events list, click OnAfterChange, and then click Edit.</li> <li> 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 == &quot;Delete&quot; && eventObj.Source.nodeName == &quot;my:group2&quot;) { //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(&quot;my:field1&quot;); //Set the selection on all nodes in Detail. var nodesDetail = XDocument.DOM.selectNodes(&quot;/my:myFields/my:group3/my:group4&quot;); //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(&quot;my:field3&quot;); //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;

} } } </li> <li>On the File menu, click Exit to close the editing window.</li> <li>Click OK to close the Field or Group Properties dialog box.</li></ol> </li></ol>

Preview the form to demonstrate how the code works.To do this, follow these steps: <ol> <li>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</li> <li>Insert some test values into the first table on the form. This table is the Master table. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In the first column of the first row, type Test1 .</li> <li>In the second column of the first row, type Master1 .</li> <li>Under the row where you just typed the data, click Insert item to add a new row to the first table.</li> <li>In the first column of the second row, type Test2 .</li> <li>In the second column of the second row, type Master2 .</li> <li>If you want to add more rows, repeat steps 2c through 2e.</li></ol> </li> <li>Insert some test values into the second table on the form. This table is the Details table. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In the first column of the first row, type Test1 .</li> <li>In the second column of the first row, type Detail1 .</li> <li>Under the row where you just typed the data, click Insert item to add a new row to the second table.</li> <li>In the first column of the second row, type Test2 .</li> <li>In the second column of the second row, type Detail2 .</li> <li>If you want to add more rows, repeat steps 2c through 2e.</li></ol> </li> <li>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.</li> <li>Delete a row from the Master table. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In the third table, select the second row where the first column contains &quot;Test2&quot;.</li> <li>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, &quot;Test2&quot; is deleted.</li></ol> </li></ol>

Keywords: kbxml kbcodesnippet kbcode kbprogramming kbhowto KB892952

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.