IIF Statement error

Posted on 2014-08-18
Last Modified: 2014-08-20
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])

Question by:donnie91910
    LVL 84

    Assisted Solution

    by:Scott McDaniel (Microsoft Access MVP - EE MVE )
    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])
    LVL 18

    Assisted Solution

    by:Richard Daneke
    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.
    LVL 47

    Accepted Solution

    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')

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now