What an Expression Box Is

 < Day Day Up > 

Chapter 4, "InfoPath Form Controls," describes almost all the InfoPath 2003 form controls. The expression box control gets its own chapter because it has some characteristics that are different from those of other controls.

In particular, an expression box control requires you to use XPath to create XPath expressions that are associated with the control. This technique allows you to display in a form view values that aren't entered by the user, but that are derived in some way from data the user enters into the view.

For example, you can automatically number an item by counting its position in an XPath node-set. Alternatively, you can sum the numbers in a column of numbers or find the average of a group of numbers .

Using an Expression Box to Compose XPath Expressions

If you don't have much experience writing XPath code, trying to get an XPath location path exactly correct can be difficult and frustrating. You can use an InfoPath expression box to help solve your problem and get the XPath expression just as you need it to be.

To illustrate the necessary technique, follow these steps:

  1. Create a new form template using the New Blank Form option in the Design a Form task pane.

  2. Drag a text box onto the blank form area.

  3. Drag an expression box onto the form area. InfoPath won't let you actually place the expression box on the form area without an XPath expression selected. The Insert Expression Box window opens.

  4. On the right side of the Insert Expression Box window is the Select XPath button, shown in Figure 5.1, which allows you to visually select a field in the data source. The button has a Select XPath ToolTip, visible when you move the mouse pointer over the button.

    Figure 5.1. The Insert Expression Box window.

    graphics/05fig01.gif

  5. Click the button. The Select a Field or Group window opens, as shown in Figure 5.2.

    Figure 5.2. Select the field whose XPath location path you are interested in.

    graphics/05fig02.gif

  6. Click the field that corresponds to the field of interest. In this simple example, it's the field1 field in the myFields group. Notice that in the Select a Field or Group window, no namespace prefix is displayed in the name of fields.

  7. Click OK to return control to the Insert Expression Box window. Notice that the XPath location path, in this case my:field1 , is entered automatically into the XPath box in the Insert Expression Box window (see Figure 5.3).

    Figure 5.3. The desired XPath location path is added automatically to the Insert Expression Box window.

    graphics/05fig03.gif

It's important to notice that the namespace prefix for the my:field1 field is not displayed in the Select a Field or Group window, but the namespace prefix must be included in the XPath location path! This seeming discrepancy makes it easy to mistakenly select a nonexistent DOM node. If that happens, you will likely get behavior other than what you expected.

For example, if you attempt to select the field1 field using the following JScript code

 xmlNode = XDocument.DOM.documentElement.selectSingleNode("field1"); 

you might expect a node to be selected. If you then use the xmlNode variable later in your JScript code, a potentially puzzling error will turn up, possibly indicating the absence of an expected object.

Use the namespace prefix appropriate to your form templatethe namespace prefix my is often used automatically by InfoPath 2003, but check the namespace declarations in the script.js or script.vbs fileso the following code would be more appropriate:

 xmlNode = XDocument.DOM.documentElement.selectSingleNode("my:field1"); 
 < Day Day Up > 


Microsoft Office InfoPath 2003 Kick Start
Microsoft Office InfoPath 2003 Kick Start
ISBN: 067232623X
EAN: 2147483647
Year: 2004
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net