How to Handle a Null Value Returned by DLookup in VBA
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.

- 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.
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.
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.
Open the VBA module and find the line where your variable is declared, such as 'Dim CurBOMCompRelDate As Date'.
Modify the declaration statement to use the Variant type. Change it to 'Dim CurBOMCompRelDate As Variant'.
Keep your existing DLookup assignment code. It will now successfully assign the Null value to the Variant variable when no matching record is found.

Test the Result Using the IsNull Function
Check if the DLookup result is Null before attempting to assign it to a strictly typed Date or String variable.
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. Download the Installer: Visit the official WPS Office website and click the download button for your operating system.
- 2. Install WPS Office: Run the downloaded installer and follow the quick setup wizard to install the software on your device.
- 3. Open and Edit: Launch WPS Spreadsheet and instantly open your existing Excel or macro-enabled workbooks to continue your work smoothly.

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.




