Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 311172 - Last Review: January 31, 2007 - Revision: 5.4
Part 5 of "Keeping Your Information Accurate" in Access 2003 and Access 2002: "Using Validation Rules to Restrict Data"
This article was previously published under Q311172
On This Page
SUMMARY
The information covered in this article is provided by:
Microsoft
Press
(http://mspress.microsoft.com/)
.
This article is part 5 of a series of eight
articles that explain how to keep your information in Access accurate. To view
the other articles in this series, please see the "Additional Resources"
section later in this article.
This information is an excerpt from
the
Microsoft Access Version 2002 Step by Step book, Chapter 6: "Keeping Your Information Accurate". Learn More
About
Microsoft
Access Version 2002 Step by Step
(http://www.microsoft.com/mspress/books/sampchap/5054.aspx)
.
MORE INFORMATION
A
validation rule is an
expression that can precisely define the information that will be accepted
in one or several fields in a record. You might use a validation rule on a
field containing the date an employee was hired to prevent a date in the future
from being entered. Or if you make deliveries to only certain local areas, you
could use a validation rule on the phone field or ZIP code field to refuse
entries from other areas.
You can type validation rules in by hand,
or you can use the
Expression Builder to create them. At the field level, Access uses the rule to test
an entry when you attempt to leave the field. At the table level, Access can
use the rule to test the content of several fields when you attempt to leave
the record. If an entry doesn't satisfy the rule, Access rejects the entry and
displays a message explaining why.
In this exercise, you will create
and test several field validation rules and one table validation rule. Follow
these steps:
- Open the Field Test database that you created in Part 2 of this series of articles.
For additional
information, click the following article number to view the article in the
Microsoft Knowledge Base:
311168Â
(http://kbalertz.com/Feedback.aspx?kbNumber=311168/
)
Keeping Your Information Accurate: Part 2: Using the Data Type to Restrict Data
- Open the Field Property Test table in Design view.
- To add a validation rule to PhoneField that will prevent the entry of an area code other than 206 or
425, start by selecting PhoneField and clicking in the Validation Rule box.
- A ... button appears at the end of the Validation Rule box. You can click this button to use the Expression Builder to
create an expression, or you can type an expression in the box. Type the
following in the Validation Rule box, and press ENTER:
Like "206*" Or Like "425*"
- In the Validation Text box, type Area code must be 206 or
425.
You have set a rule for the first three digits typed
in the PhoneField field and provided the text that Access should display if
someone attempts to enter an invalid phone number. - Click in the Caption box, and type Phone Number.
The
table window now looks like this:
Collapse this imageExpand this image
- Save the table.
Access warns you that data
integrity rules have changed. The table violates the new rule because it
contains blank phone number fields. - Click No to close the message box without testing the data.
TIP: You can test the validation rules in a table at any time by
right-clicking the title bar of the table and clicking Test Validation Rules on the shortcut menu. - Return to Datasheet view, where the caption for the first
field is now Phone Number.
- Place the insertion point to the left of the first # of any
Phone Number field, type 3605550009, and press
ENTER.
TIP: You can move the pointer to the left end of the Phone Number field and, when the pointer changes to a fat cross, click to
select the entire field. The insertion point is then at the start of the area
code when you begin typing.
The Validation Rule setting causes Access to display an alert box, warning you that
the area code must be either 206 or 425. - Click OK to close the alert box, and type a new phone number with one of
the allowed area codes.
- Return to Design view, and add another date field. Type
Date2 as the field name, set the data type to Date/Time, and drag the new field to just below DateField.
- Add a table validation rule to ensure that the second date
is always later than the first one. Right-click the table window, and click Properties on the shortcut menu to open the Table Properties dialog box, shown here:
Collapse this imageExpand this image
TIP: This dialog box is not the one you see if you right-click the
table in the database window and click Properties. The only point in common between the two is the Description property, which you can enter in either dialog box. - Click in the Validation Rule box, type [DateField] <[Date2], and
press ENTER.
- Type Date2 must be later than
DateField, and close the dialog box.
- Save the table (click No to close the data integrity alert box), and return to Datasheet
view.
- In any record, type 6/1/01 in DateField and 5/1/01 in Date2, and then click in another record.
Access displays the
Validation Text setting from the Table Properties dialog box, reminding you that Date2 must be later than DateField. - Click OK, change Date2 to 6/2/2001, and click in another
record.
- Close the table and the database.
ADDITIONAL RESOURCES
REFERENCES
The information in this article is an excerpt from the
Microsoft Access Version 2002 Step by Step book, published by Microsoft Press.
Collapse this imageExpand this image
(http://www.microsoft.com/mspress/books/sampchap/5054.aspx)
Learn More About
Microsoft
Access Version 2002 Step by Step
(http://www.microsoft.com/mspress/books/sampchap/5054.aspx)
For more information
about this publication and other Microsoft Press titles, see
http://mspress.microsoft.com
(http://mspress.microsoft.com)
.
APPLIES TO
- Microsoft Office Access 2003
- Microsoft Access 2002 Standard Edition
| kbdesign kbdatabase KB311172 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please