[AccessD] CheckDate?

Gustav Brock gustav at cactus.dk
Tue Nov 2 04:45:55 CST 2004


Hi Sander

If you operate with one format only (dd-mm-yyyy) then you can follow
this route:

1. Check that it represents a valid date with IsDate(txtExportDate)

2. If so, convert to date and reformat:

  datDate = CDate(txtExportDate)
  strDate = Format(datDate, "dd-mm-yyyy")

  Now, compare the strings:

  If StrComp(txtExportDate, strDate) = 0 Then
    ' Date string is OK.
  End If

The last step may not be necessary as it will insist on date strings
like 02-09-2004 where 2-9-04 would be valid as well.
The reformat, though, is needed to catch strings like 12-20-2004 which
IsDate() accepts as 20-12-2004.

/gustav


> I created a routine that checks if a date is correct. Can anybody review this one? I think that it's unnecessary complex.
 
> TIA
 
> Sander
> If Not IsNull(txtExportDate) Then
>         'Check month part
>         If Mid(txtExportDate, 4, 2) > 12 Then
>             'incorrect date!
>         Else
>             blnDate = True
>         End If
>         'Check day part
>         Select Case Left(txtExportDate, 2)
>         Case 31
>             If (Mid(txtExportDate, 4, 2) = 1) Or _
>             (Mid(txtExportDate, 4, 2) = 3) Or _
>             (Mid(txtExportDate, 4, 2) = 5) Or _
>             (Mid(txtExportDate, 4, 2) = 7) Or _
>             (Mid(txtExportDate, 4, 2) = 8) Or _
>             (Mid(txtExportDate, 4, 2) = 10) Or _
>             (Mid(txtExportDate, 4, 2) = 12) Then
>                 'correct day - month combi
>                 blnDate = True
>             Else
>                 'incorrect day-month combi
>                 blnDate = False
>             End If
>         Case 30
>             If (Mid(txtExportDate, 4, 2) = 4) Or _
>             (Mid(txtExportDate, 4, 2) = 6) Or _
>             (Mid(txtExportDate, 4, 2) = 9) Or _
>             (Mid(txtExportDate, 4, 2) = 11) Then
>                 'correct day - month combi
>                 blnDate = True
>             Else
>                 'incorrect day-month combi
>                 blnDate = False
>             End If
>         Case 28
>             'Check if month is february
>             If Mid(txtExportDate, 4, 2) = 2 Then
>                 'correct date
>                 blnDate = True
>             Else
>                 'incorrectdate
>                 blnDate = False
>             End If
>         Case 1 To 27
>             'correct day - month combi
>             blnDate = True
>         Case 29
>             'Check if month is february
>             If Mid(txtExportDate, 4, 2) = 2 Then
>                 'incorrect date
>                 blnDate = False
>             Else
>                 'correctdate
>                 blnDate = True
>             End If
>         Case Else
>             blnDate = False
>         End Select
>     Else
>         'Null date
>         Call MsgBox("U heeft geen exporteer datum opgegeven! Deze is verplicht.", _
>                      vbExclamation, "Onjuiste datum")
>         txtExportDate.SetFocus
>         Exit Sub
>     End If
>     If Not blnDate Then
>         Call MsgBox("De datum die u heeft opgegeven is onjuist! " _
>              & vbCrLf & "Vul een datum in met formaat: DD-MM-YYYY" _
>              & vbCrLf & "" _
>              , vbCritical, "Onjuiste datum")
>         txtExportDate.SetFocus
>         Exit Sub
>     Else




More information about the AccessD mailing list