Lesson 4.14. Validating Data
Figure 4-18. Data validation tests incoming data to make sure it conforms to a specified set of rules.
Figure 4-19. Access displays the error message you entered whenever the validation rule is broken.
Without a doubt, is the most powerful tool you can use to prevent data-entry errors. With , Access actually tests data to make sure that it conforms to what you want to appear in the table. If the incoming data doesn't meet your requirements, Access rejects it and displays an error message.
Data validation works best in number, currency, and date/time fields. You can create a validation rule for text entries, but doing so can be complicatedespecially if you want to test a lot of text variables.
There are actually two boxes that relate to data validation. They are the:
Validation Rule box: Used to specify the requirements for data entered into the field.
Validation Text box: Used to specify the message that will be displayed to the user when data that violates the validation rule is entered.
Creating data validation rules can be a little trickyyou create a data validation using the same hard-to-remember operators that you use in filters and queries. Table 4-8 contains some data validations that you can modify and use in your tables.
If necessary, open the Lesson 4 database and double-click the tblCustomers table.
The tblCustomers table opens.
Switch to Design view by clicking the View button on the toolbar.
You decide to specify that the DOB field cannot be later than today's date. (We can't have any people with birthdays in the future, can we?)
Click the DOB field.
Before we get started, you need to make sure that the DOB field is a Date/Time fieldyou should have changed this in a previous lesson.
Verify that the DOB field's Data Type is set to Date/Time.
First you need to enter a validation rule.
Click the Validation Rule box and type Date( ).
The validation rule you just entered will prevent users from entering dates later than today's date in the DOB field. Next you have to specify the error message that Access will display if someone tries to break your validation rule by entering a future date.
Click the Validation Text box and type Date must not be later than today's date.
Your table should look similar to Figure 4-18. Let's test our new data validation rule.
Click the Save button on the toolbar to save your changes, click No to close the message about testing existing data, and then click the View button to display the table in Datasheet view.
Data validation rules apply to both new and existing records. Move on to the next step and try modifying a record so that it breaks our data validation rule.
Click the DOB field for any record. Change the date to a future date that falls after today, and then press Enter.
When you try to violate the validation rule, Access displays the Validation Text you entered back in Step 5, as shown in Figure 4-19.
Click OK and press Esc to cancel the change.
Consider the following table your data validation "cheat sheet." It contains samples of the most common types of . Feel free to copy, modify, or mix and match these examples to create your own validation rules.
Table 4-8. Data Validation Examples
Must be less than 100.
Must be less than or equal to 100.
Between 1 and 10
Must be between 1 and 10.
Must not equal 0.
Must be a date before 1/1/95.
>= Date( )
Must be today's date or later.
<= Date( )
Must be today's date or earlier.
"Business" Or "Pleasure" Or "Other"
Must be "Business" or "Pleasure" or "Other."
Must have two characters.
Must have four numbers.
TO VALIDATE FIELD DATA:
MAKE SURE THE TABLE IS DISPLAYED IN DESIGN VIEW AND CLICK THE FIELD YOU WANT TO APPLY A VALIDATION RULE TO.
CLICK THE VALIDATION RULE BOX IN THE FIELD PROPERTIES SECTION.
ENTER AN EXPRESSION YOU WANT TO USE TO VALIDATE THE FIELD'S DATA (SEE TABLE 4-8).
CLICK THE VALIDATION TEXT BOX IN THE FIELD PROPERTIES SECTION.
TYPE THE TEXT THAT ACCESS WILL DISPLAY WHEN THE USER TRIES TO ENTER INCORRECT DATA FOR THE FIELD.