Professional Documents
Culture Documents
Create A Validation Rule
Create A Validation Rule
Create A Validation Rule
Data types Data types typically provide the first layer of validation. When you design a database
table, you define a data type for each field in the table, and that data type restricts what users can enter.
For example, a Date/Time field accepts only dates and times, a Currency field accepts only monetary
data, and so on.
Field sizes Field sizes provide another layer of validation. For example, if you create a field that stores
first names, you can set it to accept a maximum of 20 characters. Doing so can prevent a malicious user
from pasting in large amounts of gibberish text into the field, or it can prevent an inexperienced user
from mistakenly entering a first and last name in a field designed only to hold a first name.
Table properties Table properties provide very specific types of validation. For example, you can set
the Required property to Yes and, as a result, force users to enter a value in a field.
You can also use the Validation Rule property to require specific values, and the Validation Text property to
alert your users to any mistakes. For example, entering a rule such as >100 And <1000 in the Validation Rule
property forces users to enter values between 100 and 1,000. A rule such as [EndDate]>=[StartDate] forces
users to enter an ending date that occurs on or after a starting date. Entering text such as "Enter values between
100 and 1,000" or "Enter an ending date on or after the start date" in the Validation Text property tells users
when they have made a mistake and how to fix the error.
For the steps needed to add a validation rule to a table field, see the section Validate data during entry in table
fields, later in this article.
Input masks You can use an input mask to validate data by forcing users to enter values in a specific
way. For example, an input mask can force users to enter dates in a European format, such as
2007.04.14.
You can use some or all of those techniques to validate your data. Some of those features, such as data types,
become part of your database by default, but you can use other techniques, such as field properties, validation
rules, and input masks, at your discretion.
This article explains how to use the Validation Text and Validation Rule properties in table fields, queries,
and form controls. A complete discussion of other validation tools, such as data types, field sizes, and input
masks, is beyond the scope of this article.
For more information about data types and field sizes, see the article Modify or change the data type set for a
field. For more information about input masks, see the article Create an input mask to enter field or control
values in a specific format.
If the syntax for validation rules looks cryptic, the tables in the section What you can put in a validation rule
explain the syntax and provide some example validation rules.
You can define validation rules for tables and for controls on forms. When you define rules for tables, those
rules apply when you import data. To add validation rules to a table, you open the table in Design view and set
various table properties. To add validation rules to a form, you open the form in Design view and add rules to
the properties of individual controls.
The steps in the section Validate data during entry in table fields explain how to add validation rules to the
properties in table fields. The steps in the section Validate data during entry in forms, later in this article,
explain how to add rules to the properties in individual controls.
Your validation rules can contain expressions functions that return a single value. You can use expressions to
perform calculations, manipulate characters, or test data. When you create validation rules, you use expressions
primarily to test data. For example, an expression can check for one of a series of values, such as "Tokyo" Or
"Moscow" Or "Paris" Or "Helsinki". Expressions can also perform mathematical operations. For example,
the expression <100 forces users to enter values less than 100. The expression ([OrderDate] - [ShipDate])
calculates the number of days that elapsed between the time an order was placed and the time it shipped.
A discussion of expressions and functions is beyond the scope of this article. For more information about
expressions, see the article Create an expression. For more information about functions, see the articles on
Microsoft Office Online.
For examples of ways you can use expressions to validate data, see the section Validate data in a record.
The steps in the following sections explain how to validate data for tables, forms, queries, and import
operations.
1. In the Navigation Pane, right-click the table that you want to change, and then click Design View.
2. In the Field Name column, select the field that you want to change.
3. In the lower section of the table designer, on the General tab, select the Validation Rule property box,
and then enter your validation rule.
-or-
Click On the Data tab of the property sheet, click next to start the Expression Builder and create your
expression.
For more information about using the Expression Builder, see the article Create an expression.
Enter a rule that applies only to the field. For example, you can enter >0 to force users to enter positive values.
Keep in mind that a validation rule for a field does not reference other fields in the table. If the rule does
reference other fields, you are creating record-level validation.
4. Select the Validation Text property box and enter a validation message.
The message you enter depends on your validation rule. Keep the message short and try to explain where the
user is going wrong. To continue the example from the previous step, you could use Enter only positive
numbers as the validation text.
Note For more examples of field-level validation, see the section Validation reference, later in this article.
1. Repeat steps 1-2 in the previous section to open a table in Design view.
2. On the General tab, enter a record-level rule in the Validation Rule property box.
-or-
Click next to start the Expression Builder and create your expression.
For more information about using the Expression Builder, see the article Create an expression.
A record-level validation rule references more than one table field. For example, a rule such as
[RequiredDate]<=[OrderDate]+30 references two table fields, RequiredDate and OrderDate, and it ensures
that users enter ship dates that occur no later than 30 days after an order is entered. For more examples of
record-level validation, see the section Validation reference.
3. Click Yes to close the alert message and start the test.
4. If prompted to save your table, Click Yes.
5. You might see a variety of other alert messages as you proceed. Read the instructions in each message,
and then click Yes or No, as appropriate, to complete or stop the testing.
Note You can also test your validation rule by writing a query that tests for records that do not conform to
your validation rule. The results of such a query show you exactly which records fail to meet your validation
requirements. For example, if you set the Required property to Yes or Is Not Null, you test for fields that are
null.
Access opens a new query in Design view, and displays the Show Table dialog box.
2. In the Show Table dialog box, select the table or tables that you want to use in your query, click Add to
add them to the query, and then click Close.
The selected tables appear as windows in the upper section of the query designer.
3. In each table, double-click the fields that you want to include in your query.
-or-
Drag the fields from the table and drop them on a blank cell in the Field row in the lower section of the design
grid.
Make sure that you add the field that contains your validation rule.
4. In the Criteria cell of the field that contains your validation rule, enter the opposite of that rule.
For example, if you use BETWEEN 100 AND 1000, enter <100 OR >1000.
You can also apply a validation rule to a form control by opening the form in Design view and adding a rule to
the Validation Rule property and message text to the Validation Text property of the control. You can add
validation rules to some, but not all, form controls. The easiest way to determine if you can add a validation rule
to a control is to open the form in Design view and follow the steps in this section.
Remember that a control can have a different validation rule than the table field to which the control is bound.
When a conflict develops between validation rules, the rule defined for the table field takes precedence. Also,
remember that rules in controls and table fields can cancel each other out and thus prevent you from entering
any data at all. For example, suppose you apply the following rule to a date field in a table:
<#01/01/2007#
But you then apply this rule to the form control bound to the table field:
>=#01/01/2007#
The table field now requires values earlier than the year 2007, but the form control forces you to enter dates
after that year, thus preventing you from entering any data at all. If you try to enter data under those conditions,
Access tells you to enter dates before and after those specified by the conflicting validation rules, and you find
yourself caught in an endless loop.
The following steps explain how to add validation rules to controls, and how to lock controls and thus prevent
users from altering data.
1. In the Navigation Pane, right-click the form that you want to change, and then click Design View.
2. Right-click the control that you want to change, and then click Properties to open the property sheet for
the control.
3. Click the All tab, and then enter your validation rule in the Validation Rule property box.
-or-
For more information about using the Expression Builder, see the article Create an expression.
Lock a control
1. Follow steps 1-2 in the previous section to open the property sheet for the control that you want to lock.
2. Click the All tab, locate the Enabled and Locked property boxes, and then do one of the following:
To disable the control (make the control appear dimmed and unable to receive focus), set the
Enabled property to No.
To make the data in the control readable, but not allow users to change the data, set the Locked
property to Yes. If you set the Enabled property to No and the Locked property to Yes, the
control won't appear dimmed, but it won't be able to receive focus.
For more information about importing or linking to data, see the following articles:
Import or link to data in another Access database
Import or link to data in an Excel workbook
Import from or link to a SharePoint list
Import or link to data in a text file
The following table provides examples of field-level and record-level validation rules, plus explanatory
validation text. You can adapt these examples to fit your content.
-or-
The expressions in your validation rules don't use any special syntax. The information in this section explains
the syntax for some of the more common types of validation rules. As you proceed, remember that expressions
and functions can be very complex, and a comprehensive discussion is beyond the scope of this article.
For more information about expressions, see the article Create an expression. For more information about
functions, see the article Functions (arranged by category).
In addition to those rules, the following table shows the common arithmetic operators and provides examples of
how you can use them.
You can use any of the wildcard characters that Access provides in your validation rules. Keep in mind that
Access supports two sets of wildcard characters. Access does so because it supports two standards for
Structured Query Language (SQL), the language used to create and manage databases: ANSI-89 and ANSI-92.
Each of those standards uses a different set of wildcard characters.
By default, all .accdb and .mdb files use the ANSI-89 standard; conversely, Access projects use the ANSI-92
standard. If you are new to Access, in an Access project, the tables in your database reside on a computer
running Microsoft SQL Server, and the forms, reports and other objects reside on other computers. You can
change the ANSI standard for .accdb and .mdb files to ANSI-92, if you want.
For more information about using wildcard characters and the ANSI standards for SQL, see the article Access
wildcard character reference.