We help IT Professionals succeed at work.

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

Murray Brown
Murray Brown asked
on
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
    Else
        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
Comment
Watch Question

Software & Systems Engineer
Distinguished Expert 2019
Commented:
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
Commented:
Use the Beforeupdate event and set cancel=true if date is not valid

https://docs.microsoft.com/en-us/office/vba/api/access.form.beforeupdate-event
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks