Search notes:

VBA: null, nothing and more (or less)

nothing

nothing is a literal identifier that is only applicable to variables that are declared as objects or variant.
The value nothing represents an object whose object reference (which seems to be a memory address) is 0.
An object variable is considered nothing if its reference count is zero. For such variables, objPtr returns 0.

Checking if an object is nothing

There is no isNothing() function in VBA (see is…() functions).
An object can be tested for nothingness with the is nothing construct:
if obj is nothing then
   …
end if
The opposite is possible with not obj is nothing (not with obj is not nothing):
if not obj is nothing then
   …
end if
Also, the function typeName(obj) returns the string "Nothing" if obj is nothing.

Set obj = Nothing | IUnknown::Release

Setting an object variable to nothing (set obj = nothing) is basically the same as calling IUnknown::Release for the object.

empty

The value of a (variant) variable is empty if it has not yet been assigned a value. Thus, empty signals that the variable is not initialized.
Such a variable evaluates to 0 (zero) in numeric and to "" (empty string) in string context. Therefore, a variable that is not a variant (such as a long) cannot be empty (but empty can be assigned to such a variable).
In order to test whether a variant is empty, the special function isEmpty(…) can be used.
empty (like nothing) is a literal identifier.
See also the isMissing() function.

TODO

What is the relationship to vbEmpty?

null

The value null indicates that a variable contains invalid and/or inexistent data.
The null value can only be assigned to a variant variable.
Any operation that involves null values evaluates to null.
option explicit

sub testEmptyAndNull(var as variant)

    if isEmpty(var) then
       debug.print "| var is empty"
    end if

    if isNull(var) then
       debug.print "| var is null"
    end if

    debug.print "| var = " & var
    debug.print ""

end sub

sub main()

    dim dbl as double
    dim var as variant


  ' dbl = null ' null can only be assigned to variants
  '            ' Otherwise, a Run-time error 94 is thrown: Invalid use of Null

  ' If a variant is null can be tested with isNull:

    testEmptyAndNull var
'
'   | var is empty
'   | var =

    var = null
    testEmptyAndNull var
'
'   | var is null
'   | var =

    var = 42
    testEmptyAndNull var
'
'   | var = 42

end sub
Github repository about-VBA, path: /language/null-and-nothing/null.bas
Compare with the .NET class System.DBNull and the SQL null value.

Checking for a null value

To determine if a variable is null, the function isNull(…) is used:
if isNull(v) t hen
   …
end if

TODO

What is the relationship to vbNull.

TODO

There is even a vbNullString and a vbNullChar (Compare with System.Management.Automation.Language.NullString).

See also

isMissing()
Runtime error 94 (Invalid use of Null)
Compare VBA's isNull function with SQL Server's isNull

Index