Access validation rule tutorial and examples
One very important job in database design is keeping your data clean and well organized. A database professional would say it is important to maintain the integrity of your data. Input validation rules are an important tool that help you maintain this integrity.
Input validation rules are rules that restrict what values can be entered into a database field. You could, for example create an input validation rule that makes sure a number falls between 1 and 1000. Or you could create an input validation rule that checks if a valid e-mail address was entered. This tutorial will show you how to create your own input validation rules in Access. I will show you validation rule examples for validatinn text, numbers and dates.
Creating a basic Text validation rule
In the following steps I'll show you how to create a simple input validation rule that checks if the account number is 8 characters long and contains only numbers.
The Bank Account table above can be found in this Bank Database download. If you want to follow the exact stepts I took, download the database and open it in Access 2010.
You can follow this validation rule tutorial on video or you can follow the steps below the video.
- Open the Bank Database download in Access 2010, or create a table with a similar Text field in your own database. (Yes, the AccountNumber field is actually of type Text. This is because we are not going to use the field as a number. We are not going to add or substract account numbers).
- Open the table in Design View and select the AccountNumber field, or your own Text field.
- At the bottom of the screen find the "Validation
- In the Validation Rulefield type one of the
following validation rules.
This Text validation rule uses character classes to validate the input. It literally says "allow any value that consists of 8 number values". The [0-9] part means "a character between and including 0 and 9".
Alternatively, you could use the following validation rule.
This validation rule achieves the same result, but it uses the number sign (#), which allows only numbers. On the next page you will see more examples that use the number sign.
- In the Validation Text field type an error
message like. This message is displayed if the value entered
doesn't conform to the Validation Rule.
Account number must consist of 8 numbers
- Now, save your table and try to insert a new record in your
table. If you try to enter a new record with a AccountNumber that
consists of only 6 characters, Access will not insert the record.
Instead it will now complain!
So far for this validation rule example. On the next pages of this tutorial we will look at validation rules more closely. You will learn what validation rules can be applied to text, dates and numbers and I will conclude this tutorial with more examples.