Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

IIF Statement error

I have an IIF statement that I am trying to use in a report.  I keep getting the error of "Syntax error (comma) in query expression".

Here is my statement:

Ch_Numb: IIF([At_Code]='32' OR [At_Code]'='38', 'FE5954',IIF([At_Code]='33','FE5946',[Ch_Numb],IIF([At_Code]= Null or "",[Ch_Numb])

3 Solutions
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I believe you had an extra single quote just after the second [At_Code]:

Ch_Numb: IIF([At_Code]='32' OR [At_Code]='38', 'FE5954',IIF([At_Code]='33','FE5946',[Ch_Numb],IIF([At_Code]= Null or "",[Ch_Numb])
Richard DanekeCommented:
I agree about the extra ' that follows the second [At_Code], but there is more.
Let's examine: Ch_Numb: IIF([At_Code]='32' OR [At_Code]'='38', 'FE5954',IIF([At_Code]='33','FE5946',[Ch_Numb],IIF([At_Code]= Null or "",[Ch_Numb])  in parts:

Ch_Numb: <= names variable
IIF([At_Code]='32' OR [At_Code]'='38',  <= establishes IIF function and an OR test
 'FE5954',  <= value if true
IIF([At_Code]='33',  <= establishes IIF function for when false
'FE5946', <= value if true for At_Code = 33
[Ch_Numb]  <= value if false -- this should end the second IIF, but it continues...
,IIF([At_Code]= Null or "",[Ch_Numb])  <= so what and where does this belong?

I need a little more on intent before I can recommend a fix.
Dale FyeCommented:
I believe you have a number of issues with this statement.

1.  You have an extra [Ch_Numb] inserted in the statement (see highlighted text)
2.  You have 4 parameters for the 2nd IIF, not 3.
3.  You used improper syntax inside the last IIF to test for NULL or ""
4.  You only have 2 arguments inside the last IIF statement.  I've modified it as I think it should look (adding indentations to make it easier to read and debug.

IIF([At_Code]='32' OR [At_Code]'='38',
           IIF(NZ([At_Code], "") = "",

You might also consider using the Switch() function.  The Switch function takes pairs of information, the first item in each pair must evaluate to either True or False, the 2nd value in each pair is the value that will be returned if the first argument in the pair evaluates to True.  The function will continue to test pairs of data and return the first value that evaluates to true, which is why I like to add a final pair to my Switch functions.

I've also taken advantage of the IN (  ) clause to assess your first two potential values, and the NZ ( ) function for your last test.

Ch_Numb: SWITCH([At_Code] IN('32', '38'), 'FE5954', [At_Code]='33','FE5946', NZ([At_Code], "") = "", [Ch_Numb], true, 'Unknown')

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now