Sure, you can easily do this. What you need to do is to define a
table level validation, not a field level validation. Field level validations cannot reference other fields, but a table validation rule allows you tighter control because you can check across many fields.
You need to open the table's property sheet to get to this setting. How you get there depends upon what version you're using, but essentially you open the table in design view and open the property sheet. You should see the property sheet say "Table Properties" across the top.
Next, look for the property called Validation Rule and enter something like this:
[EndDate]<=[StartDate]+5
On the property called Validation Text, enter something like this:
The End Date cannot be greater than five days from the start date.
I highly recommend you enter a validation text, otherwise your users will see a cryptic error message if the validation fails. This validation rule will be checked before any record is saved. Save the rule changes now to the table and Access will prompt you that it needs to run the new validation rule against existing data. You'll need to correct this data.
By doing this validation rule at the table level, you've created a solid lock to prevent users from entering "bad" data no matter how they do it - directly in table datasheet, query, form, VBA code, etc. I still think it's a good idea to set up some validation at the
form level as the others have stated (because you can have much friendlier messages, instant feedback, etc.), but creating this rule at the data level provides a "fool-proof" method.
--------------------
Jeff Conrad - Access Junkie - MVP Alumni
SDET - XAS Services - Microsoft Corporation
Co-author -
Microsoft Office Access 2007 Inside Out
Presenter -
Microsoft Access 2007 Essentials
http://www.accessmvp.com/JConrad/accessjunkie.html
Access 2007 Info:
http://www.AccessJunkie.com
----------
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.mspx
----------