Checking for valid date

  • Thread starter Thread starter Paul M.
  • Start date Start date
P

Paul M.

I'm using a userform with a textbox among other things. I
want to ensure that not only does the user submit a date
in that textbox, I want to make sure it is a valid date.
If the date is invalid, I want to display a warning
message.

Thanks for the help.

Paul
 
Hi Paul

You can use the exit event like this.
It will not leave the textbox untill the textbox is a date

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(TextBox1.Text) Then
MsgBox "not a date"
Cancel = True
End If
End Sub
 
Look at the Isdate function.

Use the exit event for validating the textbox and also use the event code
for the buttons you are using to close the userform.
 
Fantastic !!!...Thanks Ron and Tom, it works like a dream.
-----Original Message-----
Hi Paul

You can use the exit event like this.
It will not leave the textbox untill the textbox is a date

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(TextBox1.Text) Then
MsgBox "not a date"
Cancel = True
End If
End Sub



--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)




"Paul M." <[email protected]> wrote in
message news:[email protected]...
 
Paul,

Here is one approach

If IsDate(Me.TextBox1.Text) Then
'do your stuff
Else
MsgBox "That's not a date"
Me.TextBox1.SetFocus
End If

Be careful though, dates are riddled with international issues. For
instance, I am in the UK, and if I enter 12/16/2003 which is really invalid,
it treats it as 16/12/2003 and is valid.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Back
Top