Results 1 to 10 of 10
  1. #1
    hiker8117 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2007
    Posts
    10

    15 IIF Statements in a Query

    I have to put in 15 nested IIF statements in a query and I know the maximum is 7.

    I have an idea about creating a VBA module but not sure of the VBA code and type of module that needs to be created and how it will work in conjunction with the query.

    I created an alias field in the query called Category and the IIF statements are:

    Category: IIF([Group] =’’100” and [DO] <>””,”Support”, IIF([Group]=”300” and [DO] =”440”,” Support”, IIF([Group]="700" and [DO]<>"","Support","Prod"))).

    I have 12 sets of criteria to enter and know there is a max of 7 IIF statements.



    Any help is appreciated.

    Regards

    Gene

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,913
    Try Switch() function instead.

    Category: Switch([Group]="100" And [DO]<>"","Support", [Group]="300" And [DO]="440","Support", [Group]="700" And [DO]<>"","Support", True,"Prod")

    Or use OR. You show three criteria that return the same value.

    Category: IIf(([Group]="100" And [DO]<>"") OR ([Group]="300" And [DO]="440") OR ([Group]="700" And [DO]<>""), "Support", "Prod")

    Category: Switch(([Group]="100" And [DO]<>"") OR ([Group]="300" And [DO]="440") OR ([Group]="700" And [DO]<>""),"Support", True,"Prod")

    Are you sure [DO] can have empty string? I never allow empty strings in fields, therefore I would test for Null.

    However, expression may be too long regardless so will need VBA custom function.

    A better approach is to build a lookup table of these conditions.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would consider using a table and joining to it in the query. It would have fields for Group and Do and the value to return. Much more dynamic/maintainable than a hard-coded formula or VBA function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,798
    I would avoid the use of the reserved word Group.
    If the values are dynamic, such that a table or hard coded values are not suitable, a function that accepts the value(s) is probably required.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,724
    I would like to see a clear statement of the requirement in plain English.
    15 nested IIFs seems like a decision of how to do something, but what exactly is the something in clear terms.
    Could be that the nested IIFs are required, but as you point out 7 seems to be the limit--- so my question stems from
    "why is your requirement beyond the defaults/limits of Access??"

    Perhaps there is more info regarding WHAT you need to achieve that can be used in deciding HOW (options) to do it in Access.

  6. #6
    hiker8117 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2007
    Posts
    10
    Thank you for your response. There are 15 groups that are unique to this file and the only Group "300" has a unique code in the DO field of "400". All of the other 14 Groups can a have a DO code from 001 up to 750. There are no null values in any of the DO fields
    which is why I put in <>"" because I wanted to catch every record in the other 14. I am looking to populate in the alias category field
    Support Services else Prod which is for production.

    Again

    Thanks for you response and will give this a try.

    Regards

    Gene

  7. #7
    hiker8117 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2007
    Posts
    10
    Thank you for your response

  8. #8
    hiker8117 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2007
    Posts
    10
    Great Idea..thank you for your response

    Regards

    Gene

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,798
    FYI: Null is not the same as "" and you cannot compare Null to anything. Thus it cannot be =, < or any other comparison.
    Without acknowledging who you are responding to, it's impossible to know which "idea" you refer to.

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,724
    @hiker8117
    I noticed that in your post #1 in your expression you are showing different characters in your "double quotes" (eg "", ””). This may just be how you have typed your post. It may not be relevant to your question, but I have had issues with different characters like this.
    As I asked in my post #5, a clear description of your requirement in plain simple English would be helpful. The key is simple, plain English with no database terms nor jargon. Tell us what your "business" is using terms that you would use to tell an 8 year old who knows nothing of you, your environment or database. Knowing what you are trying to achieve is critical before suggesting how it might be done.

    Perhaps you could also post a copy of your tables and relationships as a jpg/png.

    Good luck.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query on IF Statements MAX Date
    By hinkwale in forum Queries
    Replies: 1
    Last Post: 01-04-2015, 06:42 PM
  2. Query Criteria Using IIf Statements
    By MintChipMadness in forum Queries
    Replies: 6
    Last Post: 01-26-2013, 06:55 PM
  3. How to use multiple IIf statements in a query
    By jabadoojr in forum Queries
    Replies: 4
    Last Post: 12-17-2012, 11:05 AM
  4. Query iif statements
    By beckkks in forum Queries
    Replies: 1
    Last Post: 04-20-2012, 03:03 PM
  5. Query for IIF statements
    By SpotoR1 in forum Queries
    Replies: 2
    Last Post: 08-26-2009, 06:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums