The IsNull function is a built-in VBA function that is used to check if a given variable or expression is null or has no value. In other words, this function allows developers to determine whether an object or value exists in memory or not. This function is particularly useful when working with databases and manipulating data, as it helps in identifying and handling any null values that may be present.
VBA IsNull Function – Purpose, Syntax and Arguments
The main purpose of the IsNull function is to provide a reliable way to check the existence of objects or data, especially in situations where null values could cause errors or unexpected results. It allows for more efficient and accurate handling of data by providing a way to check for null values before executing a code or working with the data.
- varname: This is the mandatory argument that specifies the variable or expression to be evaluated by the function.
Let’s say we have a database table with information about employees, including their names, salaries, and employee IDs. However, some employees do not have a salary value assigned to them yet. We can use the IsNull function to identify these null values in the salary column and replace them with a default value. The code below shows how this can be achieved:
Dim salary As Integer
salary = 0
Do While Not rs.EOF
If IsNull(rs.Fields("Salary").Value) Then
rs.Fields("Salary").Value = salary
In this example, the IsNull function is used to check if the salary value for a particular employee is null. If it is, then the default value of 0 is assigned to the ‘salary’ variable, which is then used to overwrite the null value in the database.
Remarks and Important Notes:
- The IsNull function is a Boolean function, which means it returns either a true or false value.
- The function returns a true value (meaning the variable is null) for any data type that has no value assigned to it.
- If the given variable or expression is not null, then the function returns a false value.
- The IsNull function can also be used in combination with the ‘Not’ operator to check for non-null values.
- This function only returns true or false values, and cannot be used to check for other data types such as text or numbers.
Understanding VBA IsNull Function with Examples
Example 1: Basic Syntax
The IsNull function is used to determine whether a variable or expression is null or not. It returns a boolean value – True if the variable or expression is null and False if it is not null. The syntax for the IsNull function is:
expression can be any variable or expression that needs to be checked for null values.
Let’s look at an example of using the IsNull function in VBA:
Dim name As String
name = Null
Debug.Print IsNull(name) ' This will print "True"
In the above code, we have declared a variable name and assigned it the value Null. When the IsNull function is applied to this variable, it returns True since the variable is indeed null.
Example 2: Checking for Null Date Values
The IsNull function can also be used to check for null date values. In VBA, a null date value is represented by the number 0. This can be useful when dealing with dates in your code and you want to check if a certain date is null or not.
Here’s an example of using the IsNull function to check for null date values:
Dim projectDate As Date
Debug.Print IsNull(projectDate) ' This will print "True"
projectDate = #12/15/2022#
Debug.Print IsNull(projectDate) ' This will print "False"
In the above code, we have declared a variable projectDate of type Date. Before assigning a value to it, the IsNull function is applied and it returns True since the variable is null. After assigning a date value to the variable, the IsNull function returns False since the variable is no longer null.
Example 3: Using IsNull in Conditional Statements
The IsNull function is often used in conditional statements to check if a variable is null before executing a certain block of code. Let’s look at an example:
Dim employeeName As String
employeeName = Null
If IsNull(employeeName) Then
MsgBox "Employee name is null."
MsgBox "Employee name is: " & employeeName
In this code, we have declared a variable employeeName and assigned it a null value. The IsNull function is used in the If statement to check the value of the variable. Since the value is null, the message “Employee name is null.” will be displayed. If the variable had a non-null value, the Else block would have been executed and the message would have displayed the value of the variable.
Example 4: Using IsNull with Query Results
The IsNull function can also be used to check for null values in query results. This is particularly useful when dealing with data in databases, where certain fields may be left empty or null. Let’s consider an example where we have a table named Employees with the following fields:
- ID – Autonumber field
- FirstName – Text field
- LastName – Text field
Assuming we have the following data in the table:
In this case, we can use the IsNull function to check for null values in the FirstName field. The following code will display a message for each row where FirstName is null:
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("SELECT * FROM Employees")
Do Until rst.EOF
If IsNull(rst!FirstName) Then
MsgBox "First name is null for ID " & rst!ID
The code above opens a recordset using the SELECT statement to retrieve all the records from the Employees table. Then, using a Do Until loop, we can iterate through each record and check if the FirstName field is null. If it is, a message will be displayed with the corresponding ID value.
Example 5: IsNull vs. Is Nothing
It is important to note that the IsNull function and the Is Nothing keyword are not the same. While the IsNull function is used to check for null values, the Is Nothing keyword is used to check for Nothing values. Let’s look at an example to understand the difference:
Dim rst As Recordset
If IsNull(rst) Then
MsgBox "Recordset is null."
ElseIf rst Is Nothing Then
MsgBox "Recordset is nothing."
MsgBox "Recordset is not null or nothing."
In this code, we have declared a variable rst of type Recordset. The first If statement checks if the variable is null using the IsNull function. Since it is not null, the next ElseIf statement checks if the variable is Nothing using the Is Nothing keyword. In this case, the variable is Nothing since it has not been assigned a value yet. The last Else statement handles all other cases, where the variable is neither null nor Nothing.
The IsNull function is a valuable tool in VBA for checking null values. It can be used for simple checks, as well as in more complex scenarios involving databases and query results. By understanding the syntax and examples of the IsNull function, you can effectively use it in your code to handle null values and improve the functionality of your applications.