We help IT Professionals succeed at work.

Access Cancel update of Date field if not valid on bound form

Murray Brown
Murray Brown asked
Hi. I want to use the following code to check whether a date entered is valid.How do I cancel the update of the field if the date entered is not valid?

Private Sub DateofBirth_AfterUpdate()
    'Test if applicant is under required age.....
    Dim CurrentAge As Integer
    If oCountry = "Samoa" Then
        oModalAge = 18
    ElseIf oCountry = "Tonga" Then
        oModalAge = 21
    ElseIf oCountry = "Liberia" Then
        oModalAge = 18
    ElseIf oCountry = "Fiji" Then
        oModalAge = 21
        oModalAge = 18
    End If
    ''CurrentAge = AgeYears(#12/31/1985#)
    CurrentAge = AgeYears(Me.DateOfBirth)
    If CurrentAge < oModalAge Then
        MsgBox "Applicant must be at least " & CStr(oModalAge) & " years old!", , "CAVR - ATD": Exit Function
    End If
End Sub
Watch Question

Software & Systems Engineer
Distinguished Expert 2019
You have to switch to BeforeUpdate event in order to Cancel. (it has Cancel capability)
If you want to work with AfterUpdate you could use Undo but i think in some occasions it doesn't do the "whole job"
Partha MandayamTechnical Director
Use the Beforeupdate event and set cancel=true if date is not valid

Murray BrownASP.net/VBA/VSTO Developer