VBA & Macro Problems

How to Handle a Null Value Returned by DLookup in VBA

Amos GikundaAmos Gikunda Nov 2, 2026 868 views

Question details

The user needs to store a Null value returned by a DLookup expression in VBA without triggering the 'Invalid use of Null' error when no matching record is found.

How to Handle a Null Value Returned by DLookup in VBA
Product
Microsoft Access / VBA
Device & OS
not provided
Scenario
Running a VBA script where an Access DLookup function looks up a date, but no matching record exists, causing the function to return a Null value.
Observed behavior
Assigning the Null DLookup result to a specific variable type, such as a Date variable, causes the VBA execution to fail with an 'Invalid use of Null' error.
Before you start

Ensure you have your VBA editor open and locate the exact line of code where the DLookup function is assigning its result to a variable.

Solution 1Recommended

Declare the Variable as a Variant

Change the receiving variable's data type to Variant, as it is the only data type in VBA capable of storing Null values.

Standard data types like Date, String, or Integer cannot hold Null values. By declaring your variable as a Variant, the script can safely store the Null returned by DLookup without throwing an error.

1
Locate Variable Declaration

Open the VBA module and find the line where your variable is declared, such as 'Dim CurBOMCompRelDate As Date'.

2
Change Type to Variant

Modify the declaration statement to use the Variant type. Change it to 'Dim CurBOMCompRelDate As Variant'.

3
Assign the DLookup Result

Keep your existing DLookup assignment code. It will now successfully assign the Null value to the Variant variable when no matching record is found.

Declare the Variable as a Variant
Best Practice: Using a Variant is the cleanest approach when dealing with database functions like DLookup that frequently return Null for missing records.
Free Microsoft Office alternative

Looking for a Lightweight and Compatible Office Suite?

While Microsoft Access and VBA offer robust database logic, managing complex VBA macros and variable errors can be time-consuming. If you are looking for a fast, free, and highly compatible suite for your daily spreadsheet and document tasks, WPS Office is an excellent alternative.

  1. 1. Download the Installer: Visit the official WPS Office website and click the download button for your operating system.
  2. 2. Install WPS Office: Run the downloaded installer and follow the quick setup wizard to install the software on your device.
  3. 3. Open and Edit: Launch WPS Spreadsheet and instantly open your existing Excel or macro-enabled workbooks to continue your work smoothly.
Fully compatible with Microsoft Excel (.xlsx, .xlsm) and existing macro-enabled files.Includes advanced spreadsheet tools with built-in JS/VBA macro support.Lightweight installation package with incredibly fast application launch speed.Free to download and use with an intuitive, familiar user interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why does assigning Null to a Date variable cause an error in VBA?

In VBA, strict data types such as Date, Integer, and String are designed to hold specific types of data and allocate fixed memory. They do not have the capacity to represent a 'Null' (unknown or empty database value). Only the Variant data type is flexible enough to store Null.

Can I use the Nz function to fix the 'Invalid use of Null' error?

Yes, the Nz (Null to Zero) function is a great alternative. By wrapping your DLookup function like this: 'Nz(DLookup(...), DefaultDate)', you can specify a default value to return whenever the DLookup evaluates to Null, allowing you to safely assign the result to a Date variable.

Does DLookup always return Null if no record is found?

Yes, by design, the Microsoft Access DLookup function returns a Null value if it cannot find any record that satisfies the criteria arguments you provided. This behavior is standard and must be accommodated in your VBA code.