Share via

Error message for invalid date in text box

Anonymous
2010-12-19T15:04:46+00:00

Hi,

I have a form with a text box for a date.  How do I generate my own error message if the data entered for the date is invalid?

Thanks,

Phil

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2010-12-19T16:58:01+00:00

    You will have to use the form's error event to trap the error. 

    Private Sub Form_Error(DataErr As Integer, Response As Integer)

    Dim x As String

    Debug.Print DataErr

       x = Me.ActiveControl.Name

       If x = "TheDate" And DataErr = 2113 Then

          Response = acDataErrContinue

          MsgBox "Invalid date in TheDate entry."

       End If

    End Sub


    John Spencer Access MVP 2002-2005, 2007-2010 The Hilltop Institute University of Maryland Baltimore County

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-12-19T15:33:38+00:00

    You can also use the control's properties: Validation Rule and Validation Text.  If you need additional information on either, place your cursur in the desired property and press F1.  You can also do this at the table level for your field definition.

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2010-12-19T15:08:51+00:00

    If you set the Format property of the text box to a date format, Access will allow only valid dates to be entered. If you want to limit the dates to a range (e.g. no dates in the future), you can use the Before Update event:

    Private Sub BirthDate_BeforeUpdate(Cancel As Integer)

        If Me.BirthDate > Date Then

            MsgBox "The birthdate can't be in the future!", vbExclamation

            Cancel = True

        End If

    End Sub

    Was this answer helpful?

    0 comments No comments