In my Access VBA code, I have several
DLOOKUP functions in a sequence. They are all the same except for the value the parameter they are returning from ‘studys’.
The last one is occasionally producing the error:
invalid use of null
The line of code producing the error is:
necropsy = DLookup("[Necropsy]", "Studys", "[Primary Key] = " & ID)
The necropsy value in Study’s is a null Date occasionally but that is why I am looking it up.
In comparison the line above it:
studyEnd = DLookup("[Study End]", "Studys", "[Primary Key] = " & ID)
This code runs fine. It would never return a null value as study end is never null.
If you have
Dim necropsy As Date,
necropsy can not accept Null because Null is not a Date/Time value.
If you want to allow
necropsy to accept Null, make it a Variant instead:
Dim necropsy As Variant
A date cannot be Null in VBA.
If you simply remove the variable declaration it will be returned as a date when not null.