Dollar signs in expressions

C

Carol Giannini

Will someone please tell me what the dollar signs mean in expression
functions - as in Left$ versus Left, Chr$ versus Chr, etc. I'm probably not
looking in the right place, but can't find anything in help and don't
understand what the differences are or when I should use one or the other.
TIA!!!
 
B

Brendan Reynolds

The versions without the $ return a Variant, subtype String, while the
versions with the $ return a String. Use the version without the $ if you're
assigning the result to a Variant, use the version with the $ if you're
assigning the result to a String, but don't forget to consider the
possibility of Null values. For example ...

Private Sub Command2_Click()

Dim varTest As Variant
Dim strTest As String

'result may be Null
varTest = Left(Me.Text0, 2)
If IsNull(varTest) Then
MsgBox "varTest is Null"
Else
MsgBox "varTest is " & varTest
End If

'concatenation of vbNullString coerces result to string
strTest = Left$(Me.Text0 & vbNullString, 2)
If Len(strTest) = 0 Then
MsgBox "strTest is an empty string"
Else
MsgBox "strTest is " & strTest
End If

End Sub
 
C

Carol Giannini

Got it. THANK YOU!

Brendan Reynolds said:
The versions without the $ return a Variant, subtype String, while the
versions with the $ return a String. Use the version without the $ if you're
assigning the result to a Variant, use the version with the $ if you're
assigning the result to a String, but don't forget to consider the
possibility of Null values. For example ...

Private Sub Command2_Click()

Dim varTest As Variant
Dim strTest As String

'result may be Null
varTest = Left(Me.Text0, 2)
If IsNull(varTest) Then
MsgBox "varTest is Null"
Else
MsgBox "varTest is " & varTest
End If

'concatenation of vbNullString coerces result to string
strTest = Left$(Me.Text0 & vbNullString, 2)
If Len(strTest) = 0 Then
MsgBox "strTest is an empty string"
Else
MsgBox "strTest is " & strTest
End If

End Sub
 
Top