Handling DLookup's Null Return Value (1 Viewer)

jeffbruce

Registered User.
Local time
Today, 17:42
Joined
Jun 29, 2007
Messages
19
Hi everybody,

I'm using VB and my code is below:

quantityRegion = DLookup("[Quantity]", "tblSalesData", "[regionCode] = " & i & " AND [Year] = " & yr)

If (IsNull(quantityRegion)) Then
'nothing
Else
'bunch of code
...
End If

Sometimes quantityRegion results in a Null value, and apparently I don't know how to deal with it. How can one maintain this control logic while replacing the line If (IsNull(quantityRegion)) Then with an alternate (correct) chunk of code? Any suggestions?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:42
Joined
Aug 30, 2003
Messages
36,123
You can probably declare quantityRegion as a Variant instead of whatever you've currently declared it.
 

jeffbruce

Registered User.
Local time
Today, 17:42
Joined
Jun 29, 2007
Messages
19
Awesome, your suggestion worked. Cheers.

I guess I always assumed that if you didn't explicitly define (Dim) a variable in VB, it would be implicitly defined at compile-time as type Variant. I was wrong.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:42
Joined
Aug 30, 2003
Messages
36,123
Actually that should be true according to VBA help, but personally I always declare them explicitly.
 

Poppa Smurf

Registered User.
Local time
Tomorrow, 07:42
Joined
Mar 21, 2008
Messages
448
You could use the following code

If IsNull(DLookup("[Quantity]", "tblSalesData", "[regionCode] = " & i & " AND [Year] = " & yr)) Then
'nothing
Else
'bunch of code
...
End If
 

Fuse3k

Registered User.
Local time
Today, 17:42
Joined
Oct 24, 2007
Messages
74
You can also use the nz() function. Nz() replaces a null value with a specified value.

Nz(DLookup("[Quantity]", "tblSalesData", "[regionCode] = " & i & " AND [Year] = " & yr), 0)

This would return 0 if [Quantity] was null.

hth
 

Users who are viewing this thread

Top Bottom