Calculating Fields

From RAD Studio
Jump to: navigation, search

Go Up to Understanding Datasets Index


Using the Fields editor, you can define calculated fields for your datasets. When a dataset contains calculated fields, you provide the code to calculate those field's values in an Data.DB.TDataSet.OnCalcFields event handler.

The AutoCalcFields property determines when OnCalcFields is called. If AutoCalcFields is True, OnCalcFields is called when

  • A dataset is opened.
  • The dataset enters edit mode.
  • A record is retrieved from the database.
  • Focus moves from one visual component to another, or from one column to another in a data-aware grid control and the current record has been modified.

If AutoCalcFields is False, then OnCalcFields is not called when individual fields within a record are edited (the fourth condition above).

Warning: OnCalcFields is called frequently, so the code you write for it should be kept short. Also, if AutoCalcFields is True, OnCalcFields should not perform any actions that modify the dataset (or a linked dataset if it is part of a master-detail relationship), because this leads to recursion. For example, if OnCalcFields performs a Post, and AutoCalcFields is True, then OnCalcFields is called again, causing another Post, and so on.

When OnCalcFields executes, a dataset enters dsCalcFields mode. This state prevents modifications or additions to the records except for the calculated fields the handler is designed to modify. The reason for preventing other modifications is because OnCalcFields uses the values in other fields to derive calculated field values. Changes to those other fields might otherwise invalidate the values assigned to calculated fields. After OnCalcFields is completed, the dataset returns to dsBrowse state.

See Also