View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Akshay Bakhai Akshay Bakhai is offline
external usenet poster
 
Posts: 3
Default Userform Date validation

Nice stuff. However, I noticed a good feature in this
routine. If one was to provide an input like 151560 in
the textbox, then this routine "correctly" interprets it
as 15th day of 15th month of the year 1960. And since
there are only 12 months in 1960, the 13th month is
considered as January 1961, 14th month as February 1961,
and 15th month as March 1961.

Thus, to make matters short, 151560 results in 15/03/1961
as output of this routine.


-----Original Message-----
Hi David

In my opinion users should never be bothered with proper

input formats and annoying messageboxes.

The code below formats and validate all common date

inputs and converts to the desired format without noise
and interruptions. In
addition to all date entries that regional settings

recognize, you can use
Jan 12
and 4 to 8 digit non-delimited numbers (in european

ddmmyyyy order) like
1201
120195
12011995

Here's the code, I use it all the time in my projects.

Edit/remove the last age check part, it's a demo for you
only:

Private Sub TextBox1_Exit(ByVal Cancel As

MSForms.ReturnBoolean)
Dim iDay As Integer
Dim iMonth As Integer
Dim iYear As Integer
Dim dtDate As Date

With TextBox1
If Val(.Text) = 1000000 Then
iDay = Int(Val(.Text) / 1000000)
iMonth = Int(Val(.Text) / 10000) Mod 100
iYear = Val(.Text) Mod 10000
dtDate = DateSerial(iYear, iMonth, iDay)
.Text = Format$(dtDate, "dd/mm/yyyy")
ElseIf Val(.Text) 10000 Then
iDay = Int(Val(.Text) / 10000)
iMonth = Int(Val(.Text) / 100) Mod 100
iYear = Val(.Text) Mod 100
If iYear 30 Then
iYear = iYear + 1900
Else
iYear = iYear + 2000
End If
dtDate = DateSerial(iYear, iMonth, iDay)
.Text = Format$(dtDate, "dd/mm/yyyy")
ElseIf Val(.Text) 100 Then
iDay = Int(Val(.Text) / 100)
iMonth = Val(.Text) Mod 100
iYear = Year(Date)
dtDate = DateSerial(iYear, iMonth, iDay)
.Text = Format$(dtDate, "dd/mm/yyyy")
Else
If IsDate(.Text) = False Then
.Text = ""
Else
dtDate = DateValue(.Text)
If dtDate < 10 Then
.Text = ""
Else
.Text = Format$(dtDate, "dd/mm/yyyy")
End If
End If
End If
End With
'edit / remove from he
If dtDate 1000 Then
Select Case Year(Date) - Year(dtDate)
Case 0 To 13
'replace with proper actions:
MsgBox "too young"
Case 80 To 999
MsgBox "too old"
Case Else
End Select
End If
End Sub
--
HTH. Best wishes Harald
Excel MVP

Followup to newsgroup only please.

"David Goodall" skrev i

melding news:FFpZa.17140$R6.1487651@newsfep2-
win.server.ntli.net...
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




.