My recommendation
Use VarType
for built-in types covered by the VbVarType
enumeration. Use TypeName
for other types. I'll explain this recommendation in detail below.
Performance
The performance difference is most likely negligible, especially if you use VBA to write database applications.
VarType
The big advantage of VarType
is that it does not use magic strings: If you misspell vbDouble
, you get a compile time error (assuming that you use Option Explicit
, which you should). If you misspell "Double()"
, you code will just silently do the wrong thing.
TypeName
The advantage of TypeName
is that it also works for types which are not covered by the VbVarType
enumeration:
Dim b As New Collection
Dim a As Variant
Set a = b
Debug.Print VarType(a) ' Prints just the generic vbObject constant
Debug.Print TypeName(a) ' Prints "Collection"
Gotchas
Note that if the variable contains an object with a default property, VarType
returns the type of the value contained in the default property instead of vbObject
. Here is an example using MS Access VBA's TempVar class:
TempVars("x") = 123
Dim a As Variant
Set a = TempVars("x")
Debug.Print VarType(a) ' Prints vbInteger, the type of a.Value's current content.
' (Value is TempVar's default property)
Debug.Print TypeName(a) ' Prints "TempVar"