Results 1 to 12 of 12
  1. #1
    Sephaerius is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2013
    Posts
    62

    Building an iif() expression

    I'm building an expression, and trying to streamline the syntax. When I use nested iif()s, it works perfectly, however when I try to streamline using Or, it fails. Am I doing something wrong, or is this a limitation of the iif() function?



    Working example:
    Code:
    IIf([tbl_holds_stage1].[Account Type]="O.ER" And [tbl_holds_stage1].[ReasonForHold1]="ADM DX","MIRAMED",
    IIf([tbl_holds_stage1].[Account Type]="O.OP" And [tbl_holds_stage1].[ReasonForHold1]="ADM DX","MIRAMED",
    IIf([tbl_holds_stage1].[Account Type] Like "O.RE*" And [tbl_holds_stage1].[ReasonForHold1]="ADM DX","MIRAMED",
    IIf([tbl_holds_stage1].[Account Type]="O.ASU" And [tbl_holds_stage1].[ReasonForHold1]="ADM DX","MIRAMED",
    IIf([tbl_holds_stage1].[Account Type]="O.ER" And [tbl_holds_stage1].[ReasonForHold1] Like "DX1*","MIRAMED",
    IIf([tbl_holds_stage1].[Account Type]="O.OP" And [tbl_holds_stage1].[ReasonForHold1] Like "DX1*","MIRAMED",
    IIf([tbl_holds_stage1].[Account Type] Like "O.RE*" And [tbl_holds_stage1].[ReasonForHold1] Like "DX1*","MIRAMED",
    IIf([tbl_holds_stage1].[Account Type]="O.ASU" And [tbl_holds_stage1].[ReasonForHold1] Like "DX1*","MIRAMED",
    [teams].[JBH]))))))))
    Non-working example:
    Code:
    IIf([tbl_holds_stage1].[Account Type]="O.ER" Or 
    [tbl_holds_stage1].[Account Type]="O.OP" Or 
    [tbl_holds_stage1].[Account Type] Like "O.RE*" Or 
    [tbl_holds_stage1].[Account Type]="O.ASU" And 
    [tbl_holds_stage1].[ReasonForHold1]="ADM DX" Or 
    [tbl_holds_stage1].[ReasonForHold1] Like "DX1*","MIRAMED",
    [teams].[JBH])
    Thanks,

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    You might want to put brackets around the "And". When you mix And's and Or's then without brackets the And applies to everything.
    Or (xxx AND xxx) Or

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    If you have this many IIF, then you need to use a lookup table in a query.
    Put all these [Account Type] s in the tAcctType table, and join it to the tData table.
    No IIFs needed, no code needed.




  4. #4
    Sephaerius is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2013
    Posts
    62
    Quote Originally Posted by aytee111 View Post
    You might want to put brackets around the "And". When you mix And's and Or's then without brackets the And applies to everything.
    Or (xxx AND xxx) Or
    I'm sorry, I don't quite follow. Could you please elaborate?

  5. #5
    Sephaerius is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2013
    Posts
    62
    Quote Originally Posted by ranman256 View Post
    If you have this many IIF, then you need to use a lookup table in a query.
    Put all these [Account Type] s in the tAcctType table, and join it to the tData table.
    No IIFs needed, no code needed.
    I'm doing that, but these expressions are for specific exceptions to the overall table lookups.

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Code:
    IIf([tbl_holds_stage1].[Account Type]="O.ER" Or 
    [tbl_holds_stage1].[Account Type]="O.OP" Or 
    [tbl_holds_stage1].[Account Type] Like "O.RE*" Or 
    ([tbl_holds_stage1].[Account Type]="O.ASU" And 
    [tbl_holds_stage1].[ReasonForHold1]="ADM DX") Or 
    [tbl_holds_stage1].[ReasonForHold1] Like "DX1*","MIRAMED",
    [teams].[JBH])

  7. #7
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    To make it easier. begin with reason for hold:
    IIf([tbl_holds_stage1].[ReasonForHold1]="ADM DX" AND ([tbl_holds_stage1].[Account Type]="O.ER" OR ([tbl_holds_stage1].[Account Type]=............)
    OR [tbl_holds_stage1].[ReasonForHold1] Like "DX1*" AND (....)

    You can also use IN:
    IIf((accounttype IN("O.ER","O.OP"...) Or accounttype LIKE "O.RE*") AND (reason="ADM DX" OR reason LIKE "DK1*"),"MIRAMED",teams.JBH)

  8. #8
    Sephaerius is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2013
    Posts
    62
    Quote Originally Posted by aytee111 View Post
    Code:
    IIf([tbl_holds_stage1].[Account Type]="O.ER" Or 
    [tbl_holds_stage1].[Account Type]="O.OP" Or 
    [tbl_holds_stage1].[Account Type] Like "O.RE*" Or 
    ([tbl_holds_stage1].[Account Type]="O.ASU" And 
    [tbl_holds_stage1].[ReasonForHold1]="ADM DX") Or 
    [tbl_holds_stage1].[ReasonForHold1] Like "DX1*","MIRAMED",
    [teams].[JBH])
    I'm afraid this didn't work.

    What I'm looking for is any combination between types O.ER, O.OP, O.RE*, O.ASU, that occurs with any reason ADM DX, DX1*.
    I'm hoping to string that together in a more efficient format, rather than nesting an iif() statement for each possible combination.

  9. #9
    Sephaerius is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2013
    Posts
    62
    Quote Originally Posted by aytee111 View Post
    You can also use IN:
    IIf((accounttype IN("O.ER","O.OP"...) Or accounttype LIKE "O.RE*") AND (reason="ADM DX" OR reason LIKE "DK1*"),"MIRAMED",teams.JBH)
    This sounds promising... let me build and test that one out and I'll report back.

  10. #10
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    See my next post

  11. #11
    Sephaerius is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2013
    Posts
    62
    Quote Originally Posted by aytee111 View Post
    See my next post
    Success! The IN() did the trick. Thank you so much!

  12. #12
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The key in this is having the correct bracketing, remember in future - do not mix AND's and OR's without them.

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

Similar Threads

  1. Question in Building an Expression
    By Dmosk319 in forum Queries
    Replies: 1
    Last Post: 03-27-2014, 07:33 AM
  2. Building An Expression Help
    By Lisa Perry in forum Access
    Replies: 12
    Last Post: 06-19-2013, 10:44 AM
  3. Replies: 17
    Last Post: 01-16-2013, 08:22 PM
  4. Report Expression Building
    By KMac in forum Reports
    Replies: 7
    Last Post: 12-07-2012, 11:29 AM
  5. Building a Difficult DateDiff Expression
    By jma108 in forum Queries
    Replies: 0
    Last Post: 06-15-2009, 12:39 PM

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