I just built a simple form with 2 text boxes and a command button. The dates
are validated when the button is clicked. But the code inside of that could
be moved into any other area that is appropriate.
Private Sub CommandButton1_Click()
Dim date1 As Date
Dim date2 As Date
If Not IsDate(TextBox1) Then
MsgBox "TextBox1 does not have a date in it"
TextBox1.SetFocus
Exit Sub
End If
If Not IsDate(TextBox2) Then
MsgBox "TextBox2 does not have a date in it"
TextBox2.SetFocus
Exit Sub
End If
date1 = DateSerial(Year(TextBox1), _
Month(TextBox1), Day(TextBox1))
date2 = DateSerial(Year(TextBox2), _
Month(TextBox2), Day(TextBox2))
If date2 < date1 Then
MsgBox "Dates are in error, " & _
"2nd date must be after 1st date"
TextBox2.SetFocus
Exit Sub
End If
If Month(date2) <> Month(Now()) Then
MsgBox "Incorrect Month in TextBox2"
TextBox2.SetFocus
Exit Sub
End If
If Year(date2) <> Year(Now()) Then
MsgBox "Incorrect Year in TextBox2"
TextBox2.SetFocus
Exit Sub
End If
End Sub