How to: Create Field Validation Rules
You can create field validation rules to control the type of information a user can enter in database table fields. You can also specify a custom message to display for invalid values instead of the default error message.
Note
Make sure that field validation rules do not conflict semantically with record validation rules. Visual FoxPro does not compare the field-level and record-level expressions for consistency.
To create a field validation rule
Open the database containing the table.
Open the table in the Table Designer.
In the Table Designer, select the field you want.
In the Rule box from the Field validation area, type the validation expression you want. To build an expression, click the ellipsis (...) button.
When you are finished, click OK.
To display a custom error message, in the Message box, type the custom error message or expression you want to display for invalid values. To build an expression, click the ellipsis (...) button.
Note
Make sure to enclose message text in quotation marks ("").
When you are finished, click OK.
For more information, see Fields Tab, Table Designer.
To create a field validation rule programmatically
When creating the table using the SQL CREATE TABLE command, include the CHECK clause. To include custom error message text, include the ERROR clause.
-OR-
To edit an existing table, open the table with the USE command and then use the SQL ALTER TABLE command with the CHECK or SET CHECK clause. To include custom error message text, include the ERROR clause.
-OR-
Use the DBSETPROP( ) function to set the RuleExpression and RuleText field properties.
For more information, see CREATE TABLE - SQL Command, ALTER TABLE - SQL Command, and DBSETPROP( ) Function.
For example, suppose you want the number of items entered for a record in an order items table to be equal to or greater than 1. The following code adds a field validation rule requiring that numbers entered in the Quantity field are equal to or greater than 1 using the SET CHECK clause. The ERROR clause specifies a custom error message:
ALTER TABLE OrdItems ALTER COLUMN Quantity SET CHECK Quantity >= 1 ;
ERROR "Quantity must be a value greater than or equal to 1."
When the user attempts to enter a value less than 1, Visual FoxPro displays the custom error message and rejects the value.
See Also
Tasks
How to: Create Record Validation Rules
How to: Remove Validation Rules
Concepts
Field and Record Validation Rules