Results 1 to 10 of 10
  1. #1
    djclntn is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    476

    IIF Statement isn't working

    I'm not sure if (pardon the pun) this thread ought to be in queries or forms. On my Vehicles Info form 2-flds we're focus on is, Bodystyle" and "Drivetrain." Bodystye is a dropdown fld with 5-selection. One of the selections are, "SUV" And with most SUV'S the body style is, "4WD. So I thought why not design an IIfStment in the Drivetrain Query whereby the default value drivetrain is, "RWD", but when I select, "SUV" in body style then "4WD" comes up in the drivetrain fld. Here's the IFF statement I've tried:
    Drivetrain: Iff([Bodystyle]="Suv","4WD","RWD).

    Not to complicate things, but once someone kindly straightens out my IIF statement, I will want to add an OR to the statement, something like:
    Drivetrain: Iff([Bodystyle]="Suv","4WD","RWD":, OR Iff([Bodystyle]="Truck","4X4","RWD")

  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,521
    For starters, it's IIf, not Iff. Secondly, you didn't close the quotes on the final argument. An or would look like

    Field = "SUV" Or Field = "Truck"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    djclntn is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    476
    Yea pd, tried to jump back on & correct myself before anyone replied. Thanks for replying. lI'll work at it tomorrow; I may have more questions. Thanks

  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,521
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    djclntn is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    476
    P, I tried,
    "Drivetrain: Iif[Drivetrain]="Suv",[Bodystyle]="4WD","RWD"

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you miss:

    Quote Originally Posted by pbaldy View Post
    Field = "SUV" Or Field = "Truck"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    PMFJI,

    Not to complicate things, but once someone kindly straightens out my IIF statement, I will want to add an OR to the statement, something like:
    Drivetrain: Iff([Bodystyle]="Suv","4WD","RWD":, OR Iff([Bodystyle]="Truck","4X4","RWD")
    Try:
    Code:
    Drivetrain: IIf([Bodystyle]="Suv","4WD",IIf([Bodystyle]="Truck","4X4","RWD"))

  8. #8
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ssanfu View Post
    PMFJI,


    Try:
    Code:
    Drivetrain: IIf([Bodystyle]="Suv","4WD",IIf([Bodystyle]="Truck","4X4","RWD"))
    just out of curiosity how come after "4wd" you didn't close the first IIf with the end ) and included the second IIf in the first?

  9. #9
    djclntn is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    476
    I copied/pasted exactly what Steve & Ruegen wrote above, Drivetrain: IIf([Bodystyle]="Suv","4WD",IIf([Bodystyle]="Truck","4X4","RWD")). I get an error message saying, "The expression you entered has an invalid . (dot) or ! operator or invalid parentheses."

  10. #10
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @Ruegen
    just out of curiosity how come after "4wd" you didn't close the first IIf with the end ) and included the second IIf in the first?
    These are nested IIF() statements.

    I start with this in Notepad:

    Code:
    Drivetrain:IIF(test,True,False)
    Then I add the conditional part:

    Code:
    Drivetrain:IIF([Bodystyle]="Suv",True,False)
    Next add the "result if true":

    Code:
    Drivetrain:IIF([Bodystyle]="Suv","4WD",False)

    If the is not "SUV", is if a "Truck"?? Need another IIF() statement. So.. paste in the original IIF() statement in the "result if false" clause

    Code:
    Drivetrain:IIF([Bodystyle]="Suv","4WD",IIF(test,True,False))
    Enter the conditional part in the nested IIF():

    Code:
    Drivetrain:IIF([Bodystyle]="Suv","4WD",IIF([Bodystyle]="Truck",True,False))
    Add the "result if true":

    Code:
    Drivetrain:IIF([Bodystyle]="Suv","4WD",IIF([Bodystyle]="Truck","4X4",False))
    If the body style is not "SUV" and the bodystyle is not "truck", what should be returned??
    Now we have:

    Code:
    Drivetrain:IIF([Bodystyle]="Suv","4WD",IIF([Bodystyle]="Truck","4X4","RWD"))
    That is how I build nested IIF() statements for wueries



    In VBA, the nested IIF() would be written:

    Code:
        If [Bodystyle] = "Suv" Then
            Drivetrain = "4WD"
        ElseIf [Bodystyle] = "Truck" Then
            Drivetrain = "4X4"
        Else
            Drivetrain = "RWD"
        End If
    ------------------------------
    @djclntn

    The expression worked in my query. It has two opening parenthesis and two closing parenthesis, so no extra parenthesis .
    There is not a !, so can't be that.
    There is no . (dot) in the expression

    If you copied and pasted it, the expression should work..

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

Similar Threads

  1. Replies: 5
    Last Post: 04-20-2012, 03:54 PM
  2. If statement not working
    By gkaro in forum Queries
    Replies: 10
    Last Post: 02-15-2012, 01:58 AM
  3. IIF Statement not working...
    By LanieB in forum Queries
    Replies: 6
    Last Post: 01-05-2012, 12:55 PM
  4. Replies: 1
    Last Post: 07-30-2011, 07:58 AM
  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