Tip: Perform calculations when a button is pressed

Use a formula button to calculate results when a button is pressed instead of while the user is typing.

Apps normally perform calculations while the user is typing. Sometimes, though, you don’t want to distract users while they enter data, and only show the results once a button is pressed. Our new release makes this possible.

Here’s the Body Mass Index (BMI) sample app, modified to use a calculation button instead of calculating results right away:

Formula buttons are the tool of choice because they run so-called action formulas. These formulas can do things like send reports, copy values to the clipboard and show a different screen.

Crucially, they can also assign values to fields, using the new := operator. The new operator makes it easy to add calculation buttons, simply by associating formulas that use the := operator with the OnPress properties of formula buttons.

The modified BMI sample app above has removed the formula from the CalculatedBmi hidden field. Instead, its new calculation button uses this action formula:

CalculatedBmi := WeightInKg / HeightInM^2CalculatedBmi := WeightInKg / HeightInM^2

The calculation to the right of the := operator is the exact same formula that was previously associated with the CalculatedBmi hidden field.

This is the only modification you need to make to the BMI sample app to make it calculate its results using a button. There are still other hidden fields — Category and BmiRatio — that are not calculated explicitly by the new button. Instead, these fields rely on the CalculatedBmi field, and are updated when the button assigns a new value to it.

A calculation button can calculate several results, by separating calculations with ;;;.

This action formula explicitly assigns values not only to the CalculatedBmi field, but also to the Category and BmiRatio fields:

CalculatedBmi := WeightInKg / HeightInM^2; Category := IF(CalculatedBmi < 18.5, "underweight", IF(CalculatedBmi < 25, "normal", IF(CalculatedBmi < 30, "overweight", "obese"))); BmiRatio := (MIN(30, MAX(18.5, CalculatedBmi)) - 18.5) / (30 - 18.5)CalculatedBmi := WeightInKg / HeightInM^2;; Category := IF(CalculatedBmi < 18,5; "underweight"; IF(CalculatedBmi < 25; "normal"; IF(CalculatedBmi < 30; "overweight"; "obese")));; BmiRatio := (MIN(30; MAX(18,5; CalculatedBmi)) - 18,5) / (30 - 18,5)

Consider adding line breaks to formulas like the one above to make it easier to read, just after ;;;. To insert a line break, press Alt+Enter.

For details on how the calculations work, refer to the blog post we wrote when announcing the BMI sample app.

« Tip: Require a button to be pressed Tip: Ask users questions with action formulas »