4. Restricting Data by Using Validation Rules
A validation rule precisely defines the information that
will be accepted in one or several fields in a record. You might use a
validation rule in a field containing the date an employee was hired
to prevent a date in the future from being entered. Or if you deliver
orders to only certain local areas, you could use a validation rule on
the postal code field to refuse entries from other areas. You can
create validation rules for all data types except AutoNumber, OLE
Object, and Attachment.
You can create validation rules for individual fields or for
entire records:
-
Field
validation. At this level, Access uses the validation rule to test an
entry when you attempt to leave the field.
-
Record
validation. At this level, Access uses the rule to test the contents
of more than one field when you attempt to leave the
record.
If a field or record doesn’t satisfy the rule, Access rejects
the entry and displays a message explaining why.
You create a validation rule by building an expression. In
Access jargon, the term expression is synonymous with
formula. It is a combination of operators,
constants, functions, and identifiers that evaluates to a single
value. Access builds a formula in the format
a=b+c, where a is the result
and =b+c is the expression.
Tip
In addition to using expressions as validation rules, you can
use them to assign properties to tables or forms, to determine
values in fields or reports, to define a set of conditions that a
record must meet to be included in the result of a query, and so on.
The expression you use in a validation rule combines multiple
criteria to define a set of conditions that a value in a field must
meet in order to be a valid entry for that field. Multiple criteria
are combined using logical, comparison, and arithmetic operators.
Different types of expressions use different operators. The following
are the most common operators:
-
Logical operators
-
And
. This operator selects records that meet all the
specified criteria.
-
Or
. This operator selects records that meet at least one
of the criteria.
-
Not
. This operator selects records that don’t match the
criteria.
-
Comparison operators
-
<
. Less than
-
>
. Greater than
-
= Equal to
You can combine these basic operators to form the
following:
The Like operator is sometimes grouped with the comparison
operators and is used to test whether or not text matches a
pattern.
-
Arithmetic operators
-
+
. Add
-
-
. Subtract
-
*
. Multiply
-
/
. Divide
A related operator, & (a text form of +) is used to
concatenate (combine) two text strings.
You can type validation rules in the Validation Rule property
box by hand, or you can use a tool called the Expression
Builder to create them. The Expression Builder isn’t a
wizard; it doesn’t lead you through the process of building an
expression. It provides a hierarchical list of the most common
elements that you can include in an expression and an expression box
to build the expression in. You open the Expression Builder dialog box
by clicking the Validation button in the Field Validation group on the
Fields contextual tab, and then clicking either Field Validation Rule
or Record Validation Rule. In the dialog box, you can either select
functions, operators, and other elements from the list to copy them
into the expression box, or you can type the expression in the
expression box.
To explain a validation rule to users, you can create a message
that appears if someone tries to enter an invalid value in a field. A
well-crafted message tells users what data is expected in the field
and what format it should be entered in. For example, the message
Please enter a whole number between 1 and 99 is
more useful than Invalid entry.
In this exercise, you’ll create and test a field validation rule
and a record validation rule.
Set Up
You need the GardenCompany06 database you worked with in the
preceding exercise to complete this exercise. Open the
GardenCompany06 database, and display the FieldTest table in
Datasheet view. Then follow the steps.
-
Click the fPhone field
value, and replace the current entry with 6785550101, allowing the mask to format
the entry for you.
-
Click anywhere in the fPhone field, and on the Fields contextual tab, in the Field Validation group, click the
Validation button.
A list of options appears.
-
Click Field Validation
Rule.
The Expression Builder dialog box opens.
Let’s specify that only phone numbers in the 206 or 425 area
codes can be accepted in the fPhone field.
-
In the Expression
Elements list, click Operators; in the Expression Categories list, click
Comparison; and in the Expression Values list, double-click
Like.
The Like comparison operator is transferred to the
expression box.
-
In the expression box, type “206*” (including the quotation marks and
the asterisk). Then type a space.
Troubleshooting
Be sure to include the asterisk after 206. This wildcard
tells Access to allow any number of characters after the area
code. It is necessary because the fPhone field contains the
phone number as well as the area code.
-
In the Expression
Categories list, click Logical, and in the Expression Values list, double-click
Or.
The Or logical operator is transferred to the expression
box.
-
In the Expression
Categories list, click Comparison, and in the Expression Values list, double-click
Like.
The Expression Builder inserts <<Expr>> before
the Like operator as a placeholder for any other expressions you
might add. You can ignore this for now.
-
In the expression box, type “425*” (including the quotation marks and
the asterisk).
-
Double-click <<Expr>>, and press the
Delete key. Then delete the extra space before Like.
The expression now requires that the fPhone field include
either of two area codes.
-
Click OK to close
the Expression
Builder dialog box.
Access warns that existing data violates the new validation
rule, because the fPhone field contains a phone number that is not
in either of the required area codes.
-
Click Yes to close the
message box and keep the rule.
-
On the Fields contextual
tab, in the Field Validation
group, click the Validation
button, and then click Field Validation
Message.
The Enter Validation Message dialog box opens.
-
In the box, type Area code must be
206 or 425, and then click OK.
Access will display this message if someone attempts to
enter a phone number with an invalid area code.
-
Save the table, and then switch to Design view.
In the Field Properties area, the Validation Rule property
and Validation text property are set to the new rule and
message.
-
Switch back to Datasheet view, click anywhere in the
fPhone entry, and then press
the Home key to place the cursor at the beginning of the
field.
Tip
To select an entire field, point to the left end of the
field, and when the pointer changes to a thick cross, click the
mouse button.
-
Type 3605550109, and then
press Tab.
Access displays an alert box.
Tip
You will see the Was This Information Helpful link only if
you have enrolled in the Microsoft Customer Experience
Improvement Program. Clicking the link displays a dialog box
where you can give your opinion about the usefulness of the
validation text.
-
Click OK to close the
alert box, type a new phone number with one of the allowed area
codes, and press Tab.
Now let’s create a record validation rule that compares one
date with another to verify that it is later.
-
Scroll to the right, and click the Click to Add field. Then create a
Date/Time field with the
Medium Date format, and name
the field fDate2.
-
On the Fields contextual
tab, in the Field Validation
group, click the Validation
button, and then click Record Validation
Rule.
The Expression Builder dialog box opens. In the Expression
Elements list, the FieldTest table is selected, and its fields are
displayed in the expression Categories list.
-
In the Expression
Categories list, double-click fDate2 to insert it into the expression
box. Then double-click fDate.
The Expression Builder inserts <<Expr>> between
fDate2 and fDate.
-
In the expression box, double-click <<Expr>> to select it. Then
in the Expression Elements
list, click Operators; in the
Expression Categories list,
click Comparison; and in the
Expression Values list,
double-click >.
The greater than sign replaces <<Expr>> in the
expression.
-
Click OK to close the
Expression Builder dialog box,
and then click Yes to keep the
new rule.
-
Create a record validation message that says fDate2 must be later than fDate.
-
Save the table. Then replace the entry in the fDate field with 11/22/22.
-
Click the fDate2 field,
click the Calendar button, and
click today’s date. Then click in the record below.
Access displays an alert box containing the record
validation text.
-
Click OK, change
the value in fDate2 to
12/22/22, and then click in the
record below.
Clean Up
Close the FieldTest table. Retain the GardenCompany06 database
for use in later exercises.