Validation rules help control the data users enter into a database. They prevent invalid values and improve data accuracy. In this lesson, you will learn how to create validation rules and display custom error messages in Microsoft Access.
A validation rule is a condition that data must satisfy before Access accepts it. If the entered value does not satisfy the condition, Access rejects it.
Example:
>=0 And <=100
This rule allows numbers from 0 to 100, inclusive. Values such as 50 and 100 are accepted, while 120 is rejected.
Validation rules are useful for:
Field-level validation rules check individual fields. Table-level validation rules can compare multiple fields in the same record.
Suppose a Students table contains a Number field named Marks. Marks must be between 0 and 100.
Validation Rule:
>=0 And <=100
Validation Text:
Enter marks between 0 and 100.
| Entered Marks | Result |
|---|---|
| 0 | Accepted |
| 75 | Accepted |
| 100 | Accepted |
| -5 | Rejected |
| 120 | Rejected |
Suppose a CourseFee field stores the fee amount. Negative fees are not allowed.
Validation Rule:
>=0
Validation Text:
Course fee cannot be negative.
Examples:
Use the Currency data type for a monetary field and configure the validation rule in its Field Properties.
Suppose you want to accept ages from 18 to 60.
Validation Rule:
>=18 And <=60
Validation Text:
Age must be between 18 and 60.
This rule accepts 18, 25, and 60. It rejects 17 and 61.
Use this example only when those age limits match the actual requirements of your application.
Suppose an inventory table contains a Quantity field. The quantity must be greater than zero.
Validation Rule:
>0
Validation Text:
Quantity must be greater than zero.
This rule accepts 1, 5, and 100, but rejects 0 and negative values.
If your application needs to allow zero quantity,
use >=0 instead.
Validation rules can restrict dates according to your database requirements.
Validation Rule:
<=Date()
Validation Text:
Admission date cannot be in the future.
The Date() function returns the current date.
This rule is suitable when future admission dates are
not permitted.
Validation Rule:
>=#1/1/2026#
The date literal syntax and displayed date format can depend on Access settings. Test date rules using the regional configuration of your database.
AND requires both conditions to be true.
Example: Marks from 0 to 100.
>=0 And <=100
OR accepts a value when at least one condition is true.
Example: Discount must be 0 or 10.
=0 Or =10
This rule accepts only the numeric values 0 and 10. It rejects values such as 5 and 15.
Another example:
<10 Or >90
This rule accepts values below 10 or above 90. Values from 10 through 90 are rejected.
Text validation can restrict the values accepted in a Short Text field.
In ("ADCA","Tally")
This rule accepts ADCA or Tally as the field value.
Is Not Null
This checks that a field contains a non-Null value. If you also need to prevent empty text strings, consider the field's Required property and an appropriate validation rule for your design.
Use validation expressions appropriate for the field's data type and test them with actual data-entry scenarios.
| Field-Level Rule | Table-Level Rule |
|---|---|
| Validates one field. | Can compare multiple fields in one record. |
| Example: Marks between 0 and 100. | Example: EndDate must be on or after StartDate. |
| Configured in that field's properties. | Configured in the table's properties in Design View. |
Suppose a table has fields named StartDate and EndDate. A table-level validation rule can compare them:
[EndDate]>=[StartDate]
This rule checks that EndDate is on or after StartDate. Configure it as a table-level rule, not as a rule belonging only to one of those fields.
| Validation Rule | Required Property |
|---|---|
| Checks whether a value satisfies a condition. | Controls whether a field may contain Null. |
Example: >=0 |
Example: Required = Yes |
| Can restrict the permitted range of values. | Does not enforce uniqueness. |
Both properties can be used together. For example, a Marks field can be required and also restricted to values between 0 and 100.
Create a table named Students with these fields:
| Field Name | Data Type | Validation Rule |
|---|---|---|
| StudentID | AutoNumber | Primary key |
| StudentName | Short Text | Required = Yes |
| Marks | Number | >=0 And <=100 |
| CourseFee | Currency | >=0 |
| AdmissionDate | Date/Time | <=Date() |
Tasks:
1. Which property restricts entered values according to a condition?
2. Which rule allows marks from 0 to 100, inclusive?
3. Which property displays a custom message when validation fails?