Solved Invalid Use of Null Error (1 Viewer)

dgreen

Member
Local time
Today, 09:09
Joined
Sep 30, 2018
Messages
397
System_Acronym value is populated
System_Name is not populated

I'm trying to get the CaptureName code to run and while it knows that Me.System_Name is null, it's erroring on this line.
The code needs to handle situations where the system name is null.
The code compiles.

Code:
CaptureName = StrConv(Trim(ReplaceIllegalChars(Me.System_Acronym)), vbUpperCase) & IIf(IsNull(Me.System_Name), " ", " (" & StrConv(Trim(ReplaceIllegalChars(Me.System_Name)), vbProperCase) & ")")
 

cheekybuddha

AWF VIP
Local time
Today, 15:09
Joined
Jul 21, 2014
Messages
2,276
IIRC, IIf() evaluates both the True and False part of the expression, even if the first part evaluates to True.

Try:
Code:
CaptureName = StrConv(Trim(ReplaceIllegalChars(Me.System_Acronym)), vbUpperCase) & IIf(IsNull(Me.System_Name), " ", " (" & StrConv(Trim(ReplaceIllegalChars(Me.System_Name & "")), vbProperCase) & ")")

hth,

d
 

CJ_London

Super Moderator
Staff member
Local time
Today, 15:09
Joined
Feb 19, 2013
Messages
16,607
suggest try

Code:
CaptureName = StrConv(Trim(ReplaceIllegalChars(Me.System_Acronym)), vbUpperCase) &  " (" & StrConv(Trim(ReplaceIllegalChars(nz(Me.System_Name,"")), vbProperCase) & ")"
 

June7

AWF VIP
Local time
Today, 06:09
Joined
Mar 9, 2014
Messages
5,470
IIf() in Access (as in textbox ControlSource) and VBA will evaluate both parts regardless of which is returned by the conditional.
IIf() in SQL statement only evaluates the one that is returned by conditional.
 

cheekybuddha

AWF VIP
Local time
Today, 15:09
Joined
Jul 21, 2014
Messages
2,276
Thanks, @June7, for the clarification. It's this kind of inconsistent behaviour that is often irritating in Access. The inability to 'short-circuit' in VBA really ought to classed as a bug!

@dgreen, I'm pleased it worked for you. I had first considered the same expression as @CJ_London suggested, but I guessed you didn't want empty trailing parentheses if Me.System_Name was Null. 👍
 

Users who are viewing this thread

Top Bottom