IsNull Function

Description

Returns a Boolean value that indicates whether an expression contains no valid data (Null).

Syntax

IsNull(expression)

The required expression argument is a Variant containing a numeric expression or string expression.

Remarks

IsNull returns True if expression is Null; otherwise, IsNull returns False. If expression consists of more than one variable, Null in any constituent variable causes True to be returned for the entire expression.

The Null value indicates that the Variant contains no valid data. Null is not the same as Empty, which indicates that a variable has not yet been initialized. It is also not the same as a zero-length string (" "), which is sometimes referred to as a null string.

Important   Use the IsNull function to determine whether an expression contains a Null value. Expressions that you might expect to evaluate to True under some circumstances, such as If Var = Null and If Var <> Null, are always False. This is because any expression containing a Null is itself Null and, therefore, False.

See Also

IsArray function, IsDate function, IsEmpty function, IsError function, IsMissing function, IsNumeric function, IsObject function, TypeName function, Variant data type, VarType function.

Example

This example uses the IsNull function to determine if a variable contains a Null.

Dim MyVar, MyCheck
MyCheck = IsNull(MyVar)                ' Returns False.

MyVar = ""
MyCheck = IsNull(MyVar)                ' Returns False.

MyVar = Null
MyCheck = IsNull(MyVar)                ' Returns True.
Example (Microsoft Access)

The following example uses the IsNull function to determine whether the value of a control is Null. If it is, a message prompts the user to enter data. If the value is not Null, a message displays the value of the control.

Sub ControlValue(ctlText As Control)
    Dim strMsg As String

    ' Check that control is text box.
    If ctlText.ControlType = acTextBox Then
        ' If value of control is Null, prompt for data.
        If IsNull(ctlText.Value) Then
            strMsg = "No data in the field '" & ctlText.Name & "'." _
                    & vbCrLf & "Please enter data for this field now."
                If MsgBox(strMsg, vbQuestion) = vbOK Then
                    Exit Sub
                End If
        ' If value is not Null, display value.
        Else
            MsgBox (ctlText.Value)
        End If
    End If
End Sub
Example (Microsoft Excel)

This example creates a list of registered functions, placing each one in a separate row on Sheet1. Column A contains the full path and file name of the DLL or code resource, column B contains the function name, and column C contains the code for the argument data type.

theArray = Application.RegisteredFunctions
If IsNull(theArray) Then
    MsgBox "No registered functions"
Else
    For i = LBound(theArray) To UBound(theArray)
        For j = 1 To 3
            Worksheets("Sheet1").Cells(i, j).Formula = theArray(i, j)
        Next j
    Next i
End If