Add, subtract, multiply, and divide values
You can use expression boxes to add, subtract, multiply, and divide values, as well as perform other calculations. When you create formulas in an expression box, you refer to the fields and groups to which controls on the form are bound, rather than the controls themselves. The formulas are created using XPath, but you can follow these steps without knowing XPath.
- In design mode, place the insertion point where you want to insert the expression box.
- If the Controls task pane is not visible, click More Controls on the Insert menu.
- In the Controls task pane, click Expression Box.
- To add, subtract, multiply, or divide values in two fields, enter an expression such as one of the following in the XPath box in the Insert Expression Box dialog box.
Expression type Expression Add two fields my:field1 + my:field2 Subtract a field from another field my:field1 - my:field2 Multiply two fields my:field1 * my:field2 Divide a field by another field my:field1 div my:field2 Note In the preceding table, my:field1 and my:field2 are only examples. When you type your expression, you must use an XPath expression that identifies your specific fields.
- In the Insert Expression Box dialog box, click Select XPath
.
- In the Select a Field or Group dialog box, select the first field that you want, and then click OK.
- In the Insert Expression Box dialog box, select the expression in the XPath box, and then copy and paste it into a text editor such as Microsoft Notepad.
- After the expression in the text editor, type one of the following operators:
- To add the two fields, type +
- To subtract one field from the other, type -
- To multiple the two fields, type *
- To divide the first field by the second field, type div
- In the Insert Expression Box dialog box, click Select XPath
again.
- In the Select a Field or Group dialog box, select the second field that you want, and then click OK.
- In the Insert Expression Box dialog box, select the expression in the XPath box, and then copy and paste it into the text editor, after the operator.
- In the text editor, select the entire expression, and then copy and paste it into the XPath box in the Insert Expression Box dialog box.
- In the Insert Expression Box dialog box, click Select XPath
- To test the formula, click Preview Form on the Standard toolbar.
Note If an empty field is included in a formula that adds, subtracts, divides, or multiplies, the expression box will show the total as NaN or 1. #QNAN (Not a Number). To avoid this, you can create a default value for the fields used in the equation.