Private Sub TextBox1_Change()
Dim Char As String
Dim x As Date
Dim y As Date
Char = Right(TextBox1.Text, 1)
Select Case Len(TextBox1.Text)
Case 1 To 4, 6 To 7, 9 To 10
If Char Like "#" Then
If Len(TextBox1) = 10 Then
On Error Resume Next
x = DateValue(Right(TextBox1, 2) & "/" & Mid(TextBox1, 6, 2) & "/" & Left(TextBox1, 4))
y = DateSerial(Left(TextBox1, 4), Mid(TextBox1, 6, 2), Right(TextBox1, 2))
If Err = 0 And x = y Then
On Error GoTo 0
Exit Sub
Else
Err.Clear
On Error GoTo 0
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
MsgBox "Please enter a valid date in the form yyyy-mm-dd", vbCritical + vbOKOnly, "Error"
Exit Sub
End If
Else
Exit Sub
End If
End If
Case 5, 8
If Char Like "-" Then Exit Sub
End Select
Beep
On Error Resume Next
TextBox1.Text = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
TextBox1.SelStart = Len(TextBox1.Text)
End Sub