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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format a Textbox 1

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
is there a way to format a textbox after the textbox has lost focus. I have a date field that needs to be entered but I donot want to slow down the user. so I would like the date to be entered as 12152003 then when they tab to the next field it formats the textbox as follows 12/15/2003.

I have tryed txtDate.Text = Format(txtDate.Text, "MM/DD/YYYY")

how would I do this?

Any help would be appreciated.

Thanks

 
> txtDate.Text = Format(txtDate.Text, "MM/DD/YYYY")

code looks OK,

Where are you putting it?

I would use the lost_focus event...

But beware, if you have code in the change event, you will need to use a form level boolean variable to skip the code in the event

like
Code:
txtDate_LostFocus()

mblnIgnore = true
 txtDate.Text = Format(txtDate.Text, "MM/DD/YYYY")
mblnIgnore = false

end sub

txtDate_Change()
if mblnIgnore = false then
   'existing code here
end if
end sub


Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I get an overflow error when i do it that way matt. I agree though it would seem to be the correct way.

This works although probally not the best solution

Private Sub Text1_LostFocus()
Text1 = Format(Text1, "##/##/####")
End Sub
 
>I agree though it would seem to be the correct way

I think seem is the crucial phrase...

Having tried this, VB doesn't recognise "15122003" as a valid date as it cannot ( iassume) delimit months, days years...

I guess you have one solution, another would be a masked text box... or you could parse the string too

(oops should have tested code before posting!)

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I did try the mask edit box. with a mask of ##/##/####..but my problem with that was, there will always be a recordset with data called into the form. so the maskedit box could have a date or be empty. when calling the recordset up it screws up the mask edit box. the mask does nopt work correctly.

 
i dont think your function above is actually converting 12152003 to 12/15/2003 because it using the <and this is were i cant remeber exactly what im talking about> long (i think its a long i could be wrong) value to set the date!

ie 32186 equates too 13/02/1988
and 60000 equates too 08/04/2064
and 1000000 equates too 26/11/4637

having said this i hope someone picks up on what im trying to say and can explain it better! (im off to MSDN too try and figure out what i mean!)

good luck


If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 
i still cant find specific data on what im trying to relay, but as an example.

enter 37970 in your text box, it should return todays date (15 dec 2003)

realise this doesnt help you terribly but it explains the overflow!

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 
This is what I use to convert a century date from an AS400..I just cannot make this work with VB. the below code runs only on the SQL server.

(Substring([DateField],4,2) + '/' Substring ([DateField],6,2) + '/' + Substring ([DateField]2,2))

This works fine on my Sql server.. Just cannot make this work through VB.. I think the substring would have to be replaced with the Len command.

 
Try the Mid$() function

NewDate = Mid$(OldDate,1,2) & &quot;/&quot; & Mid$(OldDate,3,2) & &quot;/&quot; & Mid$(OldDate,5,4)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
You're welcome, and thank you. Don't forget you'll need some error checking in there as well!

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top