Results 1 to 6 of 6
  1. #1
    cec is offline Advanced Beginner
    Windows 2K Access 2003
    Join Date
    Apr 2012
    Posts
    42

    My long IIF statement is only working the first 2 not all of them

    i have a long iif statement in a query that will only populate the frist 2 iif statements:\



    Expr5: IIf([Expr1]="","",IIf([Expr1]>"00hrs 00min"<="12hrs 00min","< Days",IIf([Expr1]>"12hrs 01min"<="24hrs 00min","2 Days",IIf([Expr1]>"24hrs 01min"<="36hrs 00min","3 Days",IIf([Expr1]>"36hrs 01min"<="48hrs 00min","4 Days",IIf([Expr1]>"48hrs 01min",">5 Days")))))

    Expr1:42hrs 37min

    the iif statement gives me "< Days" and actually it gives that one to me for all of them.

    can someone help me with this?

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Well, for 2 tests you have to repeat the field, like:

    IIf([Expr1]>"00hrs 00min" And [Expr1] <="12hrs 00min"...

    That said, you can rely on the fact that Access will stop at the first True result so you can simply test for <12, <24, etc. You might find the Switch() function easier to work with than the nested IIf() functions.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    cec is offline Advanced Beginner
    Windows 2K Access 2003
    Join Date
    Apr 2012
    Posts
    42
    thanks pbaldy that did the trick now it's just not reading right, giving me the wrong expression.

    Expr5: IIf([Expr1]="","",IIf([Expr1]>"0hrs 0min" And [Expr1]<="12hrs 0min","< Days",IIf([Expr1]>"12hrs 0min" And [Expr1]<="24hrs 0min","2 Days",IIf([Expr1]>"24hrs 0min" And [Expr1]<="36hrs 0min","3 Days",IIf([Expr1]>"36hrs 0min" And [Expr1]<="48hrs 0min","4 Days",IIf([Expr1]>"48hrs 0min",">5 Days"))))))

    I have one that reads "5hrs 5min" and it returns ">5 Days"

    Some are populating right and some are not....would you know why it's doing this?

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    What is the data type of Expr1, the poorly named field? I assume it's text, since you have the values in quotes. You're getting an alphabetic comparison instead of a numeric one, so 5 hours is greater than 48 hours because 5 is greater than 4. To do comparisons like you're doing, Expr1 should be a date/time or numeric value.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Disregarding the data type of Expr1, the poorly named field, if you restructure the IIF() function, it can be shortened considerably.

    So here are your conditions:
    Code:
    if          then
    -------------------
    >48 hrs  > 5 Days
    
    >36 hrs    4 Days
    
    >24 hrs    3 Days
    
    >12 hrs    2 Days
    
    > 0 hrs   < 1 day
    If you structure the logic (conditions) to "fall through" from largest to smallest, you have fewer checks to make and the statement is simpler. For example, lets say that Expr1 = "24hrs 40mins".

    check...
    is Expr1 = "" No, then continue
    is Expr1 = "> 48hrs 0mins" No, continue
    is Expr1 = "> 36hrs 0mins" No, continue
    is Expr1 = "> 24hrs 0mins" Yes, return "3 Days" and exit function
    no need to check the remaining tests.


    The IIF() statement would be: (I think I got the tests in the correct order )
    Code:
    Expr5: IIf([Expr1]="","",IIf([Expr1]>"48hrs 0min","> 5 Days",IIf([Expr1]>"36hrs 0min","4 Days",IIf([Expr1]>"24hrs 0min","3 Days",IIf([Expr1]>"12hrs 0min","2 Days","< 1 Day"))))))
    Of course, the field type needs to be corrected......

    Just something to think about. Or maybe I'm just bent a little strange....

  6. #6
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That's the long version of what I mentioned in post 2. The Switch() function would be shorter still and easier to work with than nested IIf() functions.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. If statement not working
    By gkaro in forum Queries
    Replies: 10
    Last Post: 02-15-2012, 01:58 AM
  2. IIF Statement not working...
    By LanieB in forum Queries
    Replies: 6
    Last Post: 01-05-2012, 12:55 PM
  3. Substitute for overly-long IIf statement?
    By Captain Database ...!! in forum Queries
    Replies: 14
    Last Post: 07-14-2011, 12:00 PM
  4. linked table long query not working
    By mirthcyy in forum Queries
    Replies: 4
    Last Post: 09-23-2010, 03:16 PM
  5. Textbox IIF statement not working
    By jgelpi16 in forum Forms
    Replies: 2
    Last Post: 08-22-2010, 08:41 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