Originally Posted by
Transformer
Hi snb,
There is a difference.Check the following code:
Code:
Sub Test()
Dim dteToday As Date
Dim strToday As String
dteToday = Format(Now())
strToday = Format$(Now())
MsgBox dteToday & vbTab & TypeName(dteToday) & vbNewLine & strToday & vbTab & TypeName(strToday)
End Sub
I am afraid that does not show what you think it shows. TypeName is reporting the data type the variables were declared as, not the variable type being returned from the Format/Format$ functions. I think you will be able to see this more clearly from this modification to the above code...
Code:
Sub Test2()
Dim dteToday As Date
Dim strToday As String
dteToday = "1/23/2013"
strToday = DateSerial(2013, 1, 23)
MsgBox dteToday & vbTab & TypeName(dteToday) & vbNewLine & strToday & vbTab & TypeName(strToday)
End Sub
I am not sure there is a good way to show that Format returns a Variant with a sub-type of String and that Format$ returns a pure String without invoking some Windows API functions (which I am not really all that fluent with anymore). I would point out to snb, though, that the first link I gave was to the online documentation for Office 2000, so it was referring to VBA, not the compiled version of VB. Of course, snb seems to believe the documentation is in error while I do not. The compiled version of VB and VBA share a common core functionality and those string functions (with and without the $ sign) is part of that core... I find it hard to believe that Microsoft would have tinkered with the core functionality of the language, when there would have been no compelling reason to do so, just to make use of it in VBA. Hence, I believe my original statement in this thread to be accurate, but I cannot find a good way to "prove" it.
Bookmarks