Null

This keyword/value can only be used in conjunction with the Variant data type.
This keyword/value is used when a variable declared as variant does not contain any valid data.
This data type can be thought of as defining an unknown value.
This keyword cannot be assigned to variables of other data types.
The Null data type can be assigned explicitly to indicate that the variable has no data.
A Null value is normally used to indicate an invalid value or error.
The Null data type can only be assigned explicitly.

Dim vMyVariant As Variant 
vMyVariant = Null

IsNull Function

You can test a variable for a null value using the ISNULL
This function return True or False indicating if a variable (or expression) does not contain any data.

Dim vMyVariant As Variant 
vMyVariant = Null
If (IsNull(vMyVariant) = True) Then
   Call MsgBox("null value")
End If

You cannot use "myVariant = Null" to test for a null value


Null Values in a Expression

If any of the values in an expression have been assigned the value Null then the result will also be Null.

Dim vMyVariant1 As Variant 
Dim vMyVariant2 As Variant
Dim vMyResult As Variant

vMyVariant1 = 30
vMyVariant2 = Null
vMyResult = vMyVariant1 + vMyVariant2
If (vMyResult = Null) Then
   Debug.Print "Null"
End If

vbVarType.vbNull

This cannot be used to assign a Null Value.
If you assign a variable to vbNull you are actually assigning a value of 1 or "1".

Dim vMyVariant As Variant 
vMyVariant = Null
If (VarType(vMyVariant) = VBA.vbVarType.vbNull) Then
   Debug.Print "Null"
End If

Important

If you assign a Null value to a variable of a different data type you will generate a run-time error "Invalid use of Null".


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext