Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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('DataSourceName')/docRoot/Node1/Node2 = my:myField1

In this syntax, the placeholders are:
  • 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

  1. Start InfoPath 2003. Design a new, blank form.
  2. Select the Data Source task pane. To add fields to your form, follow these steps:
    1. In the Data Source task pane, right-click myFields, and then click Add.
    2. 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.
    3. In the Data Source task pane, right-click myExpenses, and then click Add.
    4. In the Add Field or Group dialog box, type ExpenseName, and then click OK.
    5. In the Data Source task pane, right-click myExpenses, and then click Add.
    6. In the Add Field or Group dialog box, type ExpenseValue. In the list of data types, select Decimal (double), and then click OK.
  3. Move the myExpenses group in the Data Source task pane to your form, and then click Repeating Table.

For InfoPath 2007

  1. Start InfoPath 2007.
  2. In the left pane of the Getting Started dialog box, click Design a Form Template.
  3. In the Design a Form Template window, click Blank, and then click OK.
  4. 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:
    1. In the Data Source task pane, right-click myFields, and then click Add.
    2. In the Add Field or Group dialog box, type myExpenses.
    3. In the Type, click Group.
    4. Click Repeating, and then click OK.
    5. In the Data Source task pane, right-click myExpenses, and then click Add.
    6. In the Add Field or Group dialog box, type ExpenseName, and then click OK.
    7. In the Data Source task pane, right-click myExpenses, and then click Add.
    8. In the Add Field or Group dialog box, type ExpenseValue.
    9. In the list of data types, click Decimal (double), and then click OK.

Use a Script for Conditional Formatting

For InfoPath 2003

  1. On the Tools menu, point to Script, and then click Microsoft Script Editor.
  2. Add the following function to the code window:
    function CheckItem()
    {
        return "Airfare";
    }
  3. Save the script, and then close Script Editor.
  4. On your form, right-click the text box for the ExpenseName field, and then click Text Box Properties.
  5. In the Text Box Properties dialog box, click the Display tab, and then click Conditional Formatting.

    The Conditional Formatting dialog box appears.
  6. Click Add.

    The Conditional Format dialog box appears.
  7. 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.
  8. Click OK to close the Conditional Formatting dialog box.
  9. Click OK to close the Text Box Properties dialog box.
  10. Click Preview Form. To test the form, follow these steps:
    1. Enter the expense name Lodging. Enter the amount 250.
    2. Press CTRL+ENTER to add a new row.
    3. 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

  1. On the Tools menu, point to Programming, and then click Microsoft Script Editor.
  2. Add the following function to the code window.
    function CheckItem()
    {
        return "Airfare";
    }
  3. On the File menu, click Save.
  4. On the File menu, click Exit.
  5. On the form, right-click the text box for the ExpenseName field, and then click Text Box Properties.
  6. In the Text Box Properties dialog box, click the Display tab, and then click Conditional Formatting.

    The Conditional Formatting dialog box appears.
  7. Click Add.

    The Conditional Format dialog box appears.
  8. 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()
  9. Click Bold, and then click OK.
  10. To close the Conditional Formatting dialog box, click OK.
  11. To close the Text Box Properties dialog box, click OK.
  12. On the File menu, click Preview, and then click Form.
  13. To test the form, follow these steps:
    1. Enter the expense name Lodging.
    2. Enter the amount 250.
    3. To add a new row, press CTRL+ENTER.
    4. Enter the expense name Airfare.
    5. 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

  1. Use an XML editor or a text editor such as Notepad to create an XML file with the following contents:
    <?xml version="1.0"?>
    <ExpenseRequirements>
    	<RequiresReceipt>75.00</RequiresReceipt>
    </ExpenseRequirements>
    Name the file ExpenseRequirements.xml.
  2. To add ExpenseRequirements.xml as a secondary data source for your form, follow these steps:
    1. On the Tools menu, click Convert Main Data Source.

      The Data Source Wizard appears.
    2. Click Browse.

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

    The Conditional Formatting dialog box appears.
  5. Click Add.

    The Conditional Format dialog box appears.
  6. 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("ExpenseRequirements")/ExpenseRequirements/RequiresReceipt

    In the Background Color drop-down list, select the color red, and then click OK.
  7. Click OK to close the Conditional Formatting dialog box.
  8. Click OK to close the Repeating Table Properties dialog box.
  9. Click Preview Form.
  10. To test the form, follow these steps:
    1. Enter the expense name Lunch. Enter the amount 25.
    2. Press CTRL+ENTER to add a new row.
    3. 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.

↑ Back to the top


Keywords: KB827008, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 827008
Revision : 1
Created on : 10/4/2011
Published on : 10/4/2011
Exists online : False
Views : 457