Results 1 to 10 of 10
  1. #1
    timmygrover is offline Novice
    Windows Vista Access 2003
    Join Date
    Dec 2011
    Posts
    26

    IFF statement in query with multiple outcomes

    I've seen this issue KINDA addressed elsewhere, but the answer usually seems to be, "Well, it'd be easier if you did something completely different." But I would actually like to know how to do it, and I'm sure there are others who would as well.



    SO

    Lets say you've got a field in a table called [favorite food] and you've got one person that entered BEEF, one that entered APPLE, and one that entered CARROT. And you'd like to make a query that outputs the type of food MEAT, FRUIT or VEGETABLE.

    How would you craft your IFF statement to output Meat if it's Beef, Fruit if it's Apple, or Vegetable if it's Carrot?

    IFF([favorite food]="BEEF", "MEAT", OR IFF([favorite food]="APPLE", "FRUIT", OR IFF[favorite food]="CARROT", "VEGETABLE")

    I know that's not right, but I don't know what would be either. Please help if you can! Thanks in advance

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    For starters, it's IIf, not IFF. It would be along the lines of:

    IIf([favorite food]="BEEF", "MEAT", IIf([favorite food]="APPLE", "FRUIT", IIf([favorite food]="CARROT", "VEGETABLE")))
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    Personally, I would do it in VBA easier to program and modify Assuming that Beef, Apple and Carrot aren't the only choices your IIF could get unmanageable

    Public Fuction foodtype(ByRef strvalue as string) as string

    Select Case strvalue
    case is = "Beef", Is = "Chicken", Is = "Pork"
    foodtype = "Meat"
    Case is = "Apple",is = "orange", Is = "Pear"
    foodtype = "Fruit"
    Case is = "Carrot", "Peas", "Corn"
    foodtype = "Vegetable"
    Case Else
    foodtype = "Other"
    End select

    End function

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    So would I (actually I'd have it in a table), but:

    Quote Originally Posted by timmygrover View Post
    I've seen this issue KINDA addressed elsewhere, but the answer usually seems to be, "Well, it'd be easier if you did something completely different." But I would actually like to know how to do it, and I'm sure there are others who would as well.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    timmygrover is offline Novice
    Windows Vista Access 2003
    Join Date
    Dec 2011
    Posts
    26
    @ pbaldy, sorry, you're right, (iif, not iff). I just popped it out really quick, didn't proof.

  6. #6
    timmygrover is offline Novice
    Windows Vista Access 2003
    Join Date
    Dec 2011
    Posts
    26
    Also, the situations in which I'd like to use these are actually much more specific than the food group examples i gave. I would actually like to know the construction to properly complete this type of statement.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Quote Originally Posted by timmygrover View Post
    I would actually like to know the construction to properly complete this type of statement.
    I believe I gave you that in post 2, did I not?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    timmygrover is offline Novice
    Windows Vista Access 2003
    Join Date
    Dec 2011
    Posts
    26
    Oops, you did! I was so busy feeling silly for iff/iif that I didn't even notice that you'd done it. Thanks so much for your help!

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    No problemo!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    Hi Paul, I knew that's not what he asked but you had already answered it and I thought I'd point out there are better ways to get the result he asked for. Best way as you pointed out is a table.

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

Similar Threads

  1. Multiple IF-Then-Else Statement...PLEASE HELP
    By royalrochelle in forum Programming
    Replies: 11
    Last Post: 12-08-2011, 12:14 PM
  2. iif statement with multiple conditions
    By ragsgold in forum Queries
    Replies: 7
    Last Post: 08-24-2011, 05:38 PM
  3. SQL statement from multiple tables
    By Backpacker in forum Queries
    Replies: 5
    Last Post: 06-17-2011, 11:22 PM
  4. multiple IF Statement
    By newtoAccess in forum Queries
    Replies: 3
    Last Post: 11-29-2010, 09:18 AM
  5. multiple iif statement NEED HELP PLZ
    By scott munkirs in forum Reports
    Replies: 1
    Last Post: 09-27-2006, 05:21 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