How to use calculated fields

Frequently, in addition to a simple data output, it may be necessary to perform different kinds of data manipulation, such as summation, calculation of simple average, etc.

Let's discuss a few cases based on Cars template that comes with the software. Depending on complexity of solving problem you need to make some changes on Events screen, or on Edit SQL query screen

 

1. SQL method. Adding a calculated field that gives us an average of EpaCity and EpaHighway fields

This approach works the best when you need to display the calculated value but you don't need to save it in the database.

1. Open your project and proceed to Edit SQL Query screen.

2. Switch to the SQL mode.

3. In SQL mode add string ([EPACity] + [EPAHighway])/2 as AvgEpa.

Sample SQL Query

4. Proceed to Choose fields screen.

5. For AvgEpa field unselect following check boxes: Add, Edit, Inline Add, Inline Edit.

6. Build your project and view results in the web browser.

 

2. Using server-side events. Calculating the tax when record is added or edited.

1. Click on Before Record Updated Event.

2. Modify sample event code by putting following code snippets to appropriate place.

PHPRunner Example

ASPRunner.NET Example

ASPRunnerPro Example

3. Apply the same code to Before Record Added Event.

 

3. Using Javascript to calculate tax on the fly when Price or Horsepower fields are being changed

1. Open your project and proceed to the Events screen.

2. Click on JavaScript Onload event.

3. Add following code:

Now you can edit or add new records and every time when you make changes in Horsepower or Price field correspondingly Tax field has changed. It's very comfortable because right away you see imposed tax and can modify the price depending on it.

Applies to:

  • PHPRunner
  • ASPRunner.NET
  • ASPRunnerPro

Back to top