PDA

View Full Version : Solved: Formatting dates enter in userform



clhare
07-03-2008, 04:37 AM
I have a userform where some dates are entered. I want them to be spelled out (not numeric) and I want required spaces in them so they don't split. The following code works, but is there a better way?

strEffectiveDate = txtEffectiveDate.Value
strRTWDate = txtRTWDate.Value

' Format dates, then replace spaces in them with required spaces
strEffectiveDate = Format(strEffectiveDate, "MMMM d, yyyy")
strEffectiveDate = Replace(strEffectiveDate, Chr$(32), Chr$(160))
strRTWDate = Format(strRTWDate, "MMMM d, yyyy")
strRTWDate = Replace(strRTWDate, Chr$(32), Chr$(160))

MOS MASTER
07-03-2008, 01:53 PM
Hi Cheryl, :hi:

I don't know of any other way of doing this.
The problem is that you want the non-breaking space in there for good reason's.

So I'd stick with what you have.
You can however write it on one line:

strRTWDate = Replace(Format(strRTWDate, "MMMM d, yyyy"), Chr$(32), Chr$(160))


However I doubt if it makes your code more readable. ;)

HTH

fumei
07-06-2008, 03:50 AM
Agreed. You are stuck with the structure of Format. There is no other way.

However, as you are doing this more than once, you should make a Function. This is what functions are for.


Function MyWay(strIn As String) As String
strIn = Format(strIn, "MMMM d, yyyy")
strIn = Replace(strIn, Chr$(32), Chr$(160))
MyWay = strIn
End Function


Sub Yadda()
strEffectiveDate = MyWay(txtEffectiveDate.Value)
strRTWDate = MyWay(txtRTWDate.Value)
End Sub

Now whenever you want your way of formatting , use the MyWay function.

BTW: while not invalid, I would still encourage you to use explicit properties.

If txtEffectiveDate is a textbox, then use:

txtEffectiveDate.Text

rather than

txtEffectiveDate.Value


Value (on a textbox) does indeed return the string of the current text. But so does .Text

.Value is the default property of a control, and may mean something else depending on the control.

If you mean to use the text value of something, then get the string (text) value.

As I said, it is not wrong in this particular case as the .Value of a textbox is the string of....., guess it, yes, .Text.

fumei
07-06-2008, 03:51 AM
Hi Cheryl, :hi:

I don't know of any other way of doing this.
The problem is that you want the non-breaking space in there for good reason's.

So I'd stick with what you have.
You can however write it on one line:

strRTWDate = Replace(Format(strRTWDate, "MMMM d, yyyy"), Chr$(32), Chr$(160))


However I doubt if it makes your code more readable. ;)

HTH

fumei
07-06-2008, 03:52 AM
Oooops, somehow got too many clicking away!

clhare
07-07-2008, 08:26 AM
Thank you so much!! This is very helpful!