
Originally Posted by
MrBlackd
Hi, I have a form that has a specific textbox named DateBox and I want to restrict the user from entering any date later than 25 November 2013.
Any quick ideas on how to do this?
I have searched quickly but did not manage to find something suitable.
Thanks in advance for any reply.
I have tried something like this but it is not working...
Code:
Private Sub Datebox_Change()
Dim maxdate As String
maxdate = "25/11/2013"
If Datebox.Value > maxdate Then
MsgBox "You have entered an invalid date"
Else
Exit Sub
End If
End Sub
I don't think you will want to use the Change event as it is fired for every character you type or delete... you won't have time to form your dates before you test the entry for being a date. I think you will need to use the Exit event... if the date is not proper, you can cancel the exit. Code like this should work for you...
Code:
Private Sub DateBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(DateBox.Value) Then
If IsDate(DateBox.Value) Then
If CDate(DateBox.Value) > DateSerial(2013, 11, 25) Then
MsgBox "You have entered an invalid date"
Cancel = True
End If
Else
MsgBox "You have entered an invalid date"
Cancel = True
End If
End If
End Sub
Bookmarks