How to use ISNULL function in ms-access

NIlesh Lanke picture NIlesh Lanke · May 7, 2012 · Viewed 15.2k times · Source

I have database table with following fields

FieldId FieldGroupId FieldName

I need to select FieldGroupId from Fields table where FieldId = "?" and iff FieldGroupId is empty it should return some default value.

Following query does not work if FieldID is not present in databse.

select IIf(IsNull(FieldGroupID),"AA",FieldGroupID) from Fields where FieldID ='ALPHAA'

Answer

HansUp picture HansUp · May 7, 2012

If you will be doing this from within an Access application session, you can use DLookup() to retrieve the FieldGroupID value which matches your FieldID value.

DLookup("FieldGroupID", "[Fields]", "FieldID ='ALPHAA'")

DLookup() will give you Null when FieldID is not found. You can use the Nz() function to substitute your desired value for Null.

Nz(DLookup("FieldGroupID", "[Fields]", "FieldID ='ALPHAA'"), "AA")