Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Validate a date

Hi,


I would like to validate that something that the user have writen in a
text-box in a user form realy is a date and that is has a proper format.
Does any one have any ideas ?


Thank you !

Melker


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default Validate a date

Hi Melker

A user should really be allowed to enter a date in whichever format she's
comfortable with; Excel and VBA can understand most of them and a date is a
date is a date.

Try this, it runs when the textbox looses focus. It accepts "Feb 1", "2.5
03" and whatever, if it's a valid date then it reformats to mm/dd/yyyy
format, otherwise it clears and beeps:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Dt As Date
On Error Resume Next
Dt = DateValue(TextBox1.Text)
If Dt DateSerial(1950, 1, 1) Then
TextBox1.Text = Format$(Dt, "mm/dd/yyyy")
Else
TextBox1.Text = ""
Beep
End If
End Sub

HTH. Best wishes Harald

"Melker" skrev i melding
...
Hi,


I would like to validate that something that the user have writen in a
text-box in a user form realy is a date and that is has a proper format.
Does any one have any ideas ?


Thank you !

Melker




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Validate a date

Thank you,

I will try that!

I would however want to see if i can not also "test" the date.
With that i mean that 2004-02-31 or 31/2/2004 would not be valid...

Is there a easy way ?


Regards

Melker
"Harald Staff" skrev i meddelandet
...
Hi Melker

A user should really be allowed to enter a date in whichever format she's
comfortable with; Excel and VBA can understand most of them and a date is
a
date is a date.

Try this, it runs when the textbox looses focus. It accepts "Feb 1", "2.5
03" and whatever, if it's a valid date then it reformats to mm/dd/yyyy
format, otherwise it clears and beeps:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Dt As Date
On Error Resume Next
Dt = DateValue(TextBox1.Text)
If Dt DateSerial(1950, 1, 1) Then
TextBox1.Text = Format$(Dt, "mm/dd/yyyy")
Else
TextBox1.Text = ""
Beep
End If
End Sub

HTH. Best wishes Harald

"Melker" skrev i melding
...
Hi,


I would like to validate that something that the user have writen in a
text-box in a user form realy is a date and that is has a proper format.
Does any one have any ideas ?


Thank you !

Melker






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default Validate a date

Tests first, complaints later ;-)

Ok, we may have different behaviors caused by international date formatting
differences, that is a pretty complex issue. But here in Norway february
31st is invalid to my code and the entry is discarded. Likewise, feb 29 2000
is accepted but feb 29 2100 isn't. You ought to be pretty safe.

HTH. Best wishes Harald

"Melker" skrev i melding
...
Thank you,

I will try that!

I would however want to see if i can not also "test" the date.
With that i mean that 2004-02-31 or 31/2/2004 would not be valid...

Is there a easy way ?


Regards

Melker
"Harald Staff" skrev i meddelandet
...
Hi Melker

A user should really be allowed to enter a date in whichever format

she's
comfortable with; Excel and VBA can understand most of them and a date

is
a
date is a date.

Try this, it runs when the textbox looses focus. It accepts "Feb 1",

"2.5
03" and whatever, if it's a valid date then it reformats to mm/dd/yyyy
format, otherwise it clears and beeps:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Dt As Date
On Error Resume Next
Dt = DateValue(TextBox1.Text)
If Dt DateSerial(1950, 1, 1) Then
TextBox1.Text = Format$(Dt, "mm/dd/yyyy")
Else
TextBox1.Text = ""
Beep
End If
End Sub

HTH. Best wishes Harald

"Melker" skrev i melding
...
Hi,


I would like to validate that something that the user have writen in a
text-box in a user form realy is a date and that is has a proper

format.
Does any one have any ideas ?


Thank you !

Melker








  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Validate a date

Great,
Jag provar det !

Tusen tack !

Melker

"Harald Staff" skrev i meddelandet
...
Tests first, complaints later ;-)

Ok, we may have different behaviors caused by international date
formatting
differences, that is a pretty complex issue. But here in Norway february
31st is invalid to my code and the entry is discarded. Likewise, feb 29
2000
is accepted but feb 29 2100 isn't. You ought to be pretty safe.

HTH. Best wishes Harald

"Melker" skrev i melding
...
Thank you,

I will try that!

I would however want to see if i can not also "test" the date.
With that i mean that 2004-02-31 or 31/2/2004 would not be valid...

Is there a easy way ?


Regards

Melker
"Harald Staff" skrev i meddelandet
...
Hi Melker

A user should really be allowed to enter a date in whichever format

she's
comfortable with; Excel and VBA can understand most of them and a date

is
a
date is a date.

Try this, it runs when the textbox looses focus. It accepts "Feb 1",

"2.5
03" and whatever, if it's a valid date then it reformats to mm/dd/yyyy
format, otherwise it clears and beeps:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Dt As Date
On Error Resume Next
Dt = DateValue(TextBox1.Text)
If Dt DateSerial(1950, 1, 1) Then
TextBox1.Text = Format$(Dt, "mm/dd/yyyy")
Else
TextBox1.Text = ""
Beep
End If
End Sub

HTH. Best wishes Harald

"Melker" skrev i melding
...
Hi,


I would like to validate that something that the user have writen in a
text-box in a user form realy is a date and that is has a proper

format.
Does any one have any ideas ?


Thank you !

Melker










Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
validate date entered jatman Excel Worksheet Functions 3 October 31st 09 09:07 PM
Validate date entry in a text box. GoBow777 Excel Discussion (Misc queries) 0 July 30th 08 08:46 PM
validate alternate day date tikchye_oldLearner57 Excel Discussion (Misc queries) 7 March 13th 07 06:40 PM
validate a date range tikchye_oldLearner57 Excel Discussion (Misc queries) 4 March 13th 07 01:17 AM
Get Day from Date to Validate David Excel Discussion (Misc queries) 2 August 20th 06 02:31 PM


All times are GMT +1. The time now is 02:20 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"