Hi all,
Yesterday I found the need to create an Input Mask to verify a date. After a lot of searching I didn't find one that really met my needs, so I had to write one.
Here it is:
I think it works pretty well and traps out the majority of the common errors. Hopefully it will help someone out.
I should note, this is for UK date Format, and has some error trapping to rule out future dates.
Also, I went along the route of checking each individual digit, rather than using a timer. Seemed to be a better solution.
There may be some erros, if you find any when using this it would be useful if you post them back here for everyone to see.
Yesterday I found the need to create an Input Mask to verify a date. After a lot of searching I didn't find one that really met my needs, so I had to write one.
Here it is:
Code:
Private Sub txtDate_Change()
GoTo ValCheck:
ErMsg:
MsgBox ("Incorrect date")
txtDate.Value = vbNullString
Exit Sub
ValCheck:
With txtDate
Select Case Len(.Value)
Case 1
If .Value > 3 Then
GoTo ErMsg
End If
Case 2
Select Case Left(.Value, 1)
Case Is = 3
If Mid(.Value, 2, 1) > 1 Then
GoTo ErMsg
End If
Case Else
If Mid(.Value, 2, 1) > 9 Then
GoTo ErMsg
End If
End Select
Case 3
If Not Mid(.Value, 3, 1) = "/" Then
GoTo ErMsg
End If
Case 4
Select Case Mid(.Value, 4, 1)
Case Is > 1
GoTo ErMsg
Case Else
End Select
Case 5
Select Case Mid(.Value, 5, 1)
Case 9
If Mid(.Value, 1, 2) > 30 Then
GoTo ErMsg
End If
Case 4
If Mid(.Value, 1, 2) > 30 Then
GoTo ErMsg
End If
Case 6
If Mid(.Value, 1, 2) > 30 Then
GoTo ErMsg
End If
End Select
If Mid(.Value, 1, 2) > 30 And Mid(.Value, 4, 2) = 11 Then
GoTo ErMsg
End If
If Mid(.Value, 4, 1) = 1 And Mid(.Value, 5, 1) > 2 Then
GoTo ErMsg
End If
Case 6
If Not Mid(.Value, 6, 1) = "/" Then
GoTo ErMsg
End If
Case 10
If Mid(.Value, 7, 4) < 1990 Or Mid(.Value, 7, 4) > CInt(Year(Now())) Then
GoTo ErMsg
End If
Case Is > 10
GoTo ErMsg
End Select
If Len(.Value) = 10 Then
If CDate(.Value) > Date Then
MsgBox ("Date is in Future")
End If
End If
End With
End Sub
I think it works pretty well and traps out the majority of the common errors. Hopefully it will help someone out.
I should note, this is for UK date Format, and has some error trapping to rule out future dates.
Also, I went along the route of checking each individual digit, rather than using a timer. Seemed to be a better solution.
There may be some erros, if you find any when using this it would be useful if you post them back here for everyone to see.