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:
DataSourceName refers to the name that you give to the secondary data source.
docRoot is the XML document root.
Node1 and Node2 are nodes that are contained in the XML. Node2 is a child of Node1.
my:myField1 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 Node2 in the secondary data source against my:myField1 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.
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.
In the Add Field or Group dialog box, type ExpenseValue. In the list of data types, select Decimal (double), and then click OK.
Move the myExpenses group in the Data Source task pane to your form, and then click Repeating Table.
For InfoPath 2007
Start InfoPath 2007.
In the left pane of the Getting Started dialog box, click Design a Form Template.
In the Design a Form Template window, click Blank, and then click OK.
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:
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, click Group.
Click Repeating, 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.
In the Add Field or Group dialog box, type ExpenseValue.
In the list of data types, click Decimal (double), and then click OK.
Use a Script for Conditional Formatting
For InfoPath 2003
On the Tools menu, point to Script, and then click Microsoft Script Editor.
Add the following function to the code window:
function CheckItem()
{
return "Airfare";
}
Save the script, and then close Script Editor.
On your form, right-click the text box for the ExpenseName field, and then click Text Box Properties.
In the Text Box Properties dialog box, click the Display tab, and then click Conditional Formatting.
The Conditional Formatting dialog box appears.
Click Add.
The Conditional Format dialog box appears.
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.
Click OK to close the Conditional Formatting dialog box.
Click OK to close the Text Box Properties dialog box.
Click Preview Form. To test the form, follow these steps:
Enter the expense name Lodging. Enter the amount 250.
Press CTRL+ENTER to add a new row.
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.
For InfoPath 2007
On the Tools menu, point to Programming, and then click Microsoft Script Editor.
Add the following function to the code window.
function CheckItem()
{
return "Airfare";
}
On the File menu, click Save.
On the File menu, click Exit.
On the form, right-click the text box for the ExpenseName field, and then click Text Box Properties.
In the Text Box Properties dialog box, click the Display tab, and then click Conditional Formatting.
The Conditional Formatting dialog box appears.
Click Add.
The Conditional Format dialog box appears.
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()
Click Bold, and then click OK.
To close the Conditional Formatting dialog box, click OK.
To close the Text Box Properties dialog box, click OK.
On the File menu, click Preview, and then click Form.
To test the form, follow these steps:
Enter the expense name Lodging.
Enter the amount 250.
To add a new row, press CTRL+ENTER.
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.
Use a Field in a Secondary Data Source for Conditional Formatting
Use an XML editor or a text editor such as Notepad to create an XML file with the following contents: