sharky12345
Well-known Member
- Joined
- Aug 5, 2010
- Messages
- 3,422
- Office Version
- 2016
- Platform
- Windows
I'm using this code for a Textbox event;
The problem I am having is that is the user enters a date that isn't valid I want the cursor to go back into the Textbox so they can try again, but everything I have tried does not work.
For info, the Age function is here, just in case that has something to do with it;
Code:
Private Sub TextDOB_AfterUpdate()
If TextDOB.Value = "" Then
Exit Sub
Else
If IsDate(Me.TextDOB.Value) Then
Me.Label5.Caption = Age(CDate(Me.TextDOB.Value), Date)
Else
Me.Label5.Caption = ""
Call MsgBox(TextDOB.Value & " is not a valid date of birth - please re-enter", vbCritical, "DOB error")
TextDOB.Value = ""
TextDOB.SetFocus
Cancel = True
Exit Sub
End If
End If
End Sub
The problem I am having is that is the user enters a date that isn't valid I want the cursor to go back into the Textbox so they can try again, but everything I have tried does not work.
For info, the Age function is here, just in case that has something to do with it;
Code:
Function Age(Date1 As Date, Date2 As Date) As String
Dim Y As Integer
Dim M As Integer
Dim D As Integer
Dim Temp1 As Date
Temp1 = DateSerial(Year(Date2), Month(Date1), Day(Date1))
Y = Year(Date2) - Year(Date1) + (Temp1 > Date2)
M = Month(Date2) - Month(Date1) - (12 * (Temp1 > Date2))
D = Day(Date2) - Day(Date1)
If D < 0 Then
M = M - 1
D = Day(DateSerial(Year(Date2), Month(Date2), 0)) + D
End If
Age = "Age: " & Y '& " years" ')" '& M & " months " & D & " days"
End Function