Share via


Knowing When Field-Level Rules Are Checked

Field-level rules are checked when the field's value changes. Unlike triggers, field-level rules fire even if data is buffered. When you work with data in a Browse window, form, or other window, Visual FoxPro checks field-level rules as you move away from the field. If a field value has not been changed, the rule is not checked. This means that you are free to tab through fields without the system validating any of the data.

Field-level Rule Checking

Data entry method Window or command Field-level rule checked
User interface Browse window
Form
Other window
As you move away from the field, if the field value has changed. (If the field value has not been changed, the rule is not checked.)
Commands that do not specify fields APPEND
APPEND GENERAL
APPEND MEMO
BROWSE
CHANGE
DELETE
EDIT
GATHER
As field value changes, in field definition order.
  APPEND BLANK
INSERT
INSERT - SQL
As the record is appended or inserted.
Commands that specify fields UPDATE
UPDATE - SQL
REPLACE
In the order in which fields are specified in the command.

Validating Record-Level Values

You use record-level validation rules to control the type of information a user can enter into a record. Record-level validation rules typically compare the values of two or more fields in the same record to make sure they follow the business rules established for the database. For example, you can use a record-level validation rule to ensure that one field's value is always greater than that of another in the same record.

To create a record-level validation rule and custom error message

  • In the Table tab of the Table Designer, enter the rule and error message you want in the Rule and Message boxes.

    -or-

  • Use the CHECK clause of the CREATE TABLE or ALTER TABLE commands.

For example, you might want to ensure employees are 18 years or older when hired. The following code adds a record-level validation rule and error text for the employee table requiring that the date of hire entered into the hire_date column is greater than or equal to their birth date plus 18 years:

ALTER TABLE employee SET CHECK ;
   hire_date >= birth_date + (18 * 365.25) ;
   ERROR "Employees must be 18 years or older by date of hire"

If the user enters an employee record with an invalid date, Visual FoxPro displays an error with the custom error message you defined, and does not update the record.

You can also use the SET CHECK clause of the ALTER TABLE command to create a record-level validation rule. You should ensure that any rules specified for fields do not conflict semantically with the rules you define for the table. Visual FoxPro makes no attempt to compare the field-level and record-level expressions for consistency.

See Also

Enforcing Business Rules | Record-Level Rule Checking | Working with Tables | Trigger Usage | Creating Triggers | Restricting Access to Fields