View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_5_] Bob Phillips[_5_] is offline
external usenet poster
 
Posts: 620
Default Userform Date validation

In fact, here it is
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox1
If Not ValidDate(.Text) Then
MsgBox "Please supply a valid date in dd/mm/yyyy format",
vbCritical + vbOKOnly
Cancel = True
End If
End With
End Sub

Function ValidDate(dte As String)
If Len(dte) < 10 Or Mid(dte, 3, 1) < "/" Or Mid(dte, 6, 1) < "/" Then
ValidDate = False
Exit Function
End If
If Mid(dte, 4, 2) 12 Then
ValidDate = False
Exit Function
End If
If CLng(Right(dte, 4)) < 1900 Then
ValidDate = False
Exit Function
End If
ValidDate = True
On Error GoTo invalid_date
IsDate (CDate(dte))
Exit Function
invalid_date:
ValidDate = False
End Function


--
HTH

-------

Bob Phillips
... looking out across Poole Harbour to the Purbecks


"Bob Phillips" wrote in message
...
David,

Try this. It works okay with UK & US dates on my machine. If you want to
eliminate US dates, just yell and I'll add to it.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox1
If Not ValidDate(.Text) Then
MsgBox "Please supply a valid date in dd/mm/yyyy format",
vbCritical + vbOKOnly
Cancel = True
End If
End With
End Sub

Function ValidDate(dte As String)
If Len(dte) < 10 Or Mid(dte, 3, 1) < "/" Or Mid(dte, 6, 1) < "/"

Then
ValidDate = False
Exit Function
End If
If CLng(Right(dte, 4)) < 1900 Then
ValidDate = False
Exit Function
End If
ValidDate = True
On Error GoTo invalid_date
IsDate (CDate(dte))
Exit Function
invalid_date:
ValidDate = False
End Function


--
HTH

-------

Bob Phillips
... looking out across Poole Harbour to the Purbecks


"David Goodall" wrote in message
...
Hi,
I'm currently developing a userform which has a text box that prompts

the
user for a date of birth. I would like to validate the input as a

correct
date of birth. ie not born before 1900 and in correct dd/mm/yyyy format.

I've had a look at the the isdate function but I'm not sure if it can

handle
the British date format.

Any help would be greatly appreciated.

David