6.9.4 Multiple Calculations

You can place several calculated fields on a single data entry form. In Figure 3, there are three lines of units and each fields, plus a grandtotal field that sums all the amount fields calculated from the units and each fields.

Form Name ex3

Data File

Beginning form no Unique records no

Repeating form no Appended form no

Freeze form no Additional form no

Required form no for one record

Next form name ex3

gifs/00000001.gif

Field name units1 Field length 8

Entry Allowed yes Input required no

In Data File no Invisible no

Edit Data Type real

Field name each1 Field length 12

Entry Allowed yes Input required no

In Data File no Invisible no

Edit Data Type money

Field name amount 1 Field length 14

Entry Allowed no Input required no

In Data File no Invisible no

Calculation round (units1*each1)

Edit Data Type money

Field name units2 Field length 8

Entry Allowed yes Input required no

In Data File no Invisible no

Edit Data Type real

Field name each2 Field length 12

Entry Allowed yes Input required no

In Data File no Invisible no

Edit Data Type money

Field name amount2 Field length 14

Entry Allowed no Input required no

In Data File no Invisible no

Calculation round (units2*each2)

Edit Data Type money

Field name units3 Field length 8

Entry Allowed yes Input required no

In Data File no Invisible no

Edit Data Type real

Field name each3 Field length 12

Entry Allowed yes Input required no

In Data File no Invisible no

Edit Data Type money

Field name amount3 Field length 14

Entry Allowed no Input required no

In Data File no Invisible no

Calculation round (units3*each3)

Edit Data Type money

Field name grandtotal Field length 14

Entry Allowed no Input required no

In Data File no Invisible no

Calculation amount1+amount2+amount3

Edit Data Type money

Figure 3

When any field is changed on the screen, all other fields whose value depends on that field are recalculated. Changing a units field changes the value of the corresponding amount field and also changes the value of the grandtotal field.

Note that you need not specify the order in which fields are recalculated. Form reevaluates all the fields that depend on the units field and changes those field values. In this case, an amount field depends on the units field. Form then reevaluates all fields that depend on the changed amount field. This process is continued until all the necessary fields are reevaluated.

An expression that refers to itself is not allowed. For example, the amount field may not have the expression

amount+units*each

entered in the Copy/Calculate field. A set of calculations that references itself is also invalid. The units field could not have the expression

amount+3

In this (nonsense) example, the units field depends on the amount field for its value, but the amount field already depends on the units field for its value. If expressions that reference themselves or each other are entered, the Circular calculations error message is displayed when form tries to calculate the expression.

A more likely example of circular calculations is the following calculation in a field named subtotal:

subtotal+item

The intent is to take the contents of a field named item and add it to the subtotal field. Since performing the calculation changes the value of the subtotal field, the calculation for the subtotal field would be performed again, which would again change the value of the subtotal field. Such a process would never stop, so circular calculations are not allowed.

To implement the subtotaling example above, use the following formula instead:

+=item

This will update the subtotal field only once and not cause circular calculations.