Saturday 5 June 2010

Null values in Select function - VBA Access

I have been asked about how you can trigger an event in a SELECT CASE when there is a null value. (As you can not have null as a case)

The answer is actually incredibly easy, you use the nz() function, NZ returns a variable you can set when there is a null value, it needs to arguments.

1. the field to be evaluated
2. What to replace the Null value with

nz([Field to be Evaluated],[Value to Return if Null])

in this quick example I want to return a message box if the field is null.

SELECT CASE nz(me.nullfield,0) 'This will replace a null value with a 0
CASE 0
msgbox "Error, value Needed"
CASE "Value 1"
msgbox "Value 1 Selected"
CASE "Value 2"
msgbox "Value 2 Selected"
END SELECT

obviously if "0" is one of your CASE values you can replace it with a string i.e. "None"

No comments:

Post a Comment