Home » excel » sql – Why is my DLookup producing "invalid use of null" Error: 94

sql – Why is my DLookup producing "invalid use of null" Error: 94

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

What’s up?

How to&Answers:

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

Answer:

A date cannot be Null in VBA.

If you simply remove the variable declaration it will be returned as a date when not null.