Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert number to date 4

Status
Not open for further replies.

obulldog27

Programmer
Apr 26, 2004
169
US
I have a textbox where a user would enter a number:

example: 121204

when the event is lostfocus I want to number to change to

12-12-2004

how can i do this? The date will not be fixed it can change
 
Have you thought of useing a maskededit control?

This would save all the formatting info with a non formatted entry.

At that point no problme..

Otherwise look at the left mid and right and use them to format the data.,,

i.e.

dim strFormattedDate as string
strformatteddate = left(textbox.text,2) & "-" & mid(textbox.text,3,2) & "-" & right(textbox.text,2)



 
Another vote for a masked edit control. A date-picker control would be even better, from a data integrity standpoint, but might have usability issues if the app is designed to be used by heads-down keyboard entry.

Chip H.



____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I will try the left/right mid and see how that works.

Yes the app is designed to be used by head-down keyboard
entry.

Thanks
 
it works good on the left/right mid put when
the user enters. 12104 it gets converted to 12-10-04 but should have been 12-1-04
 
The other possible problem with "12104" is

do you mean 12-01-04?

or do you mean 01-21-04?

if you don't insist on the mmddyy format then you can't be sure what is intended.

I'm with the masked edit crowd (and in good company too) on this one.
 
Require that the string is 6 or 8 characters long by using the Len function in the validate event. If you don’t, how would you to interpret 12104?

1/21/04
Or
12/1/04

If the string was required to be 6 character it would have to be 120104 or 012104, problem solved.


Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
I agree with the in good company too statement but am I the only one that thinks the Masked edit control looks a bit cheesy?

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Ok, so the logical solution would be to force the entry of an 8 digit date input (to prevent any issues with date in the future).
Ex: mmddyyyy

I have used a few methods to accomplish the same thing you are trying to do. One of the more effective ones was to create three boxes for each date with specified lengths... and make the user enter the data correctly (using validation) to proceed.
Ex: [mm]/[dd]/[yyyy]
check for valid input by doing:
Dim sTemp as String
sTemp = Text1.Text & "/" & Text2.Text & "/" & Text3.Text
If IsDate(sTemp) = True Then
'Do something...
Else
'Reject Input...
End If

Another option would be to put a format request in small letters near the box and do a validation function to verify that it is infact a date (IsDate()).
Ex: [mm/dd/yyyy]
Then Simarly to the other option:

If IsDate(Text1.Text) = True Then
'Do something...
Else
'Reject Input...
End If


Just a couple of ideas.
 
Thanks for everyone's help.

This is what I ended up using.

Private Sub add_moveindate_LostFocus()
Dim strFormatteDate As String
If IsDate(add_moveindate.Text) Then
Exit Sub
Else
strFormattedDate = Left(add_moveindate.Text, 2) & "-" & Mid(add_moveindate.Text, 3, 2) & "-" & Right(add_moveindate.Text, 2)
add_moveindate.Text = strFormattedDate
End If
End Sub
 
DrJavaJoe

No. You're not the only one who thinks masked edit looks cheesy. I would use a date picker control for this. I really don't want to be in the business of writing wadges (is that a word?) of code to validate dates when someone much smarter than me has already done it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top