Link to home
Start Free TrialLog in
Avatar of Tom Farrar
Tom FarrarFlag for United States of America

asked on

Validation Rule for table field

I have created an Access table for contracts that has two fields identifying the (contract) start date and the (contract) end date.  I thought I could have a validation in the table design for the "end date" that says < "start date", but it doesn't seem to want to let me do this.  The error (though cryptic to me) say:

"Invalid SQL Syntax - Can't use multiple columns in a column-level CHECK constraint".

What have I done wrong, and how can I make that validation?  Thanks.  - Tom
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

You cannot compare column values that way in a table-level validation rule.

What you should do instead is use a form for data entry/edit, and use the form's BeforeUpdate event
to perform the validation:

Private Sub Form_BeforeUpdate(Cancel As Integer)

    If [EndDate] < [StartDate] Then
        Cancel = True
        MsgBox "Invalid end date", vbCritical, "No soup for you!"
    End If

End Sub
Avatar of Tom Farrar

ASKER

Thanks, Patrick.  Not having done that before, is that as simple as going to the Form's properties, selecting BeforeUpdate>Event and pasting the code into the window?  - Tom
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That should get 'er done.  Thanks, Patrick....