Microsoft KB Archive/827008

= How to use a script or a secondary data source field for conditional formatting in InfoPath =

Article ID: 827008

Article Last Modified on 1/30/2007

-

APPLIES TO


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

-



IN THIS TASK

 * SUMMARY
 * Design a New Form
 * Use a Script for Conditional Formatting
 * Use a Field in a Secondary Data Source for Conditional Formatting



SUMMARY
This article describes how to use a script for a conditional formatting expression and how to use a field in a secondary data source for a conditional formatting expression. You use either the script or the field in the secondary data source in a Microsoft Office InfoPath 2007 form or in a Microsoft Office InfoPath 2003 form.

To use either a script function with conditional formatting or a secondary data source field with conditional formatting, you select the expression for the condition type.

An expression that references a field in a secondary data source has syntax that is similar to the following:

xdXDocument:GetDOM(' ')/ / / =

In this syntax, the placeholders are:
 * refers to the name that you give to the secondary data source.
 * is the XML document root.
 * and  are nodes that are contained in the XML. Node2 is a child of Node1.
 * refers to a field in the primary data source of your form.

In this particular example, the conditional formatting statement checks the text value of  in the secondary data source against   in the main data source, and then applies the format that you selected if the two values match.

Similarly, to call a script function from the conditional formatting expression, you can use the xdExtension prefix that is bound to the http://schemas.microsoft.com/office/infopath/2003/xslt/extension namespace.

back to the top

Design a New Form
This example describes how to create a conditional format that uses both expression types.

For InfoPath 2003
 Start InfoPath 2003. Design a new, blank form. Select the Data Source task pane. To add fields to your form, follow these steps:  In the Data Source task pane, right-click myFields, and then click Add. In the Add Field or Group dialog box, type myExpenses. In the Type drop-down list, select Group. Select the Repeating option, and then click OK. In the Data Source task pane, right-click myExpenses, and then click Add. In the Add Field or Group dialog box, type ExpenseName, and then click OK. In the Data Source task pane, right-click myExpenses, and then click Add.</li> In the Add Field or Group dialog box, type ExpenseValue. In the list of data types, select Decimal (double), and then click OK.</li></ol> </li> Move the myExpenses group in the Data Source task pane to your form, and then click Repeating Table.</li></ol>

For InfoPath 2007
<ol> Start InfoPath 2007.</li> In the left pane of the Getting Started dialog box, click Design a Form Template.</li> In the Design a Form Template window, click Blank, and then click OK.</li> In the Design Tasks task pane, click Data Source, and then add fields to the form. To add fields to the form, follow these steps: <ol style="list-style-type: lower-alpha;"> In the Data Source task pane, right-click myFields, and then click Add.</li> In the Add Field or Group dialog box, type myExpenses .</li> In the Type, click Group.</li> Click Repeating, and then click OK.</li> In the Data Source task pane, right-click myExpenses, and then click Add.</li> In the Add Field or Group dialog box, type ExpenseName, and then click OK.</li> In the Data Source task pane, right-click myExpenses, and then click Add.</li> In the Add Field or Group dialog box, type ExpenseValue .</li> In the list of data types, click Decimal (double), and then click OK.</li></ol> </li></ol>

back to the top

For InfoPath 2003
<ol> On the Tools menu, point to Script, and then click Microsoft Script Editor.</li> <li> Add the following function to the code window: function CheckItem {   return &quot;Airfare&quot;; } </li> <li>Save the script, and then close Script Editor.</li> <li>On your form, right-click the text box for the ExpenseName field, and then click Text Box Properties.</li> <li>In the Text Box Properties dialog box, click the Display tab, and then click Conditional Formatting.

The Conditional Formatting dialog box appears.</li> <li>Click Add.

The Conditional Format dialog box appears.</li> <li>In the Conditional Format dialog box, select the expression from the drop-down list, and then type the following condition in the text box:

my:ExpenseName = xdExtension:CheckItem

Click Bold, and then click OK.</li> <li>Click OK to close the Conditional Formatting dialog box.</li> <li>Click OK to close the Text Box Properties dialog box.</li> <li>Click Preview Form. To test the form, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Enter the expense name Lodging. Enter the amount 250 .</li> <li>Press CTRL+ENTER to add a new row.</li> <li>Enter the expense name Airfare. Enter the amount 700.

Note Only the expense name on the second row is bold because the expense name equals Airfare, and Airfare is the text value that is returned from the CheckItem function.</li></ol> </li></ol>

For InfoPath 2007
<ol> <li>On the Tools menu, point to Programming, and then click Microsoft Script Editor.</li> <li> Add the following function to the code window. function CheckItem {   return &quot;Airfare&quot;; } </li> <li>On the File menu, click Save.</li> <li>On the File menu, click Exit.</li> <li>On the form, right-click the text box for the ExpenseName field, and then click Text Box Properties.</li> <li>In the Text Box Properties dialog box, click the Display tab, and then click Conditional Formatting.

The Conditional Formatting dialog box appears.</li> <li>Click Add.

The Conditional Format dialog box appears.</li> <li>In the Conditional Format dialog box, click the expression in the list, and then type the following condition in the box:

my:ExpenseName = xdExtension:CheckItem

</li> <li>Click Bold, and then click OK.</li> <li>To close the Conditional Formatting dialog box, click OK.</li> <li>To close the Text Box Properties dialog box, click OK.</li> <li>On the File menu, click Preview, and then click Form.</li> <li>To test the form, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Enter the expense name Lodging .</li> <li>Enter the amount 250 .</li> <li>To add a new row, press CTRL+ENTER.</li> <li>Enter the expense name Airfare .</li> <li>Enter the amount 700.

Note Only the expense name on the second row is bold because the expense name equals Airfare, and Airfare is the text value that is returned from the CheckItem function.</li></ol> </li></ol>

back to the top

Use a Field in a Secondary Data Source for Conditional Formatting
<ol> <li> Use an XML editor or a text editor such as Notepad to create an XML file with the following contents: <?xml version=&quot;1.0&quot;?> <ExpenseRequirements> <RequiresReceipt>75.00</RequiresReceipt> </ExpenseRequirements> Name the file ExpenseRequirements.xml. </li> <li>To add ExpenseRequirements.xml as a secondary data source for your form, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>On the Tools menu, click Convert Main Data Source.

The Data Source Wizard appears.</li> <li>Click Browse.

The Open dialog box appears.</li> <li>Move to ExpenseRequirements.xml, and then click Open.</li> <li>Click Next, and then click Finish to complete the Data Source Setup Wizard.</li></ol> </li> <li>Right-click the repeating table on your form, and then click Repeating Table Properties.</li> <li>In the Repeating Table Properties dialog box, select the Display tab, and then click Conditional Formatting.

The Conditional Formatting dialog box appears.</li> <li>Click Add.

The Conditional Format dialog box appears.</li> <li>In the Conditional Format dialog box, select the expression in the drop-down list. Type the following condition in the text box:

my:ExpenseValue > xdXDocument:GetDOM(&quot;ExpenseRequirements&quot;)/ExpenseRequirements/RequiresReceipt

In the Background Color drop-down list, select the color red, and then click OK.</li> <li>Click OK to close the Conditional Formatting dialog box.</li> <li>Click OK to close the Repeating Table Properties dialog box.</li> <li>Click Preview Form.</li> <li>To test the form, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Enter the expense name Lunch. Enter the amount 25 .</li> <li>Press CTRL+ENTER to add a new row.</li> <li>Enter the expense name Dinner. Enter the amount 80.

Note Only the second row is formatted with a red background because the expense value exceeds 75.</li></ol> </li></ol>

back to the top

Additional query words: IP2007

Keywords: kbhowtomaster KB827008

-

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

© Microsoft Corporation. All rights reserved.