Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    69

    Iif statement in a query

    Hi all,


    I have the below statement in an Access Query, but I receive an error message. I am not sure what causes the error as I am sure the statement syntax is correct:

    Click image for larger version. 

Name:	Error Message.jpg 
Views:	32 
Size:	17.1 KB 
ID:	50498

    Click image for larger version. 

Name:	Display.jpg 
Views:	32 
Size:	19.5 KB 
ID:	50499

    Thanks

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Don't see anything wrong with expression.
    Post the actual SQL statement.

    Suggest you use a more informative alias field name than "Expr1"
    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
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    There is something wrong with that picture. Access would capitalize Iif as IIf.
    You could try removing the calculated field and see if it runs at all. I suspect the issue is somewhere else - especially if you wrote/copied the sql. IIRC, the message is often due to missing separators of one kind or another.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Is that actually a lowercase L? lif
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Quote Originally Posted by Welshgasman View Post
    Is that actually a lowercase L? lif
    If it is, it would raise 'undefined function' error, unless of course you had such a function within scope of the query.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    69
    See the updated Syntax. Once I press Enter, I receive the message and the "Yes" is highlighted. The "iif" has not yet been capatilized by Access.
    Access is not allowing me to view the SQL statement. As soon as I move the focus from the "Expr1" field, the error message is displayed.
    Is it possible that my Access app may be corrupted?


    Click image for larger version. 

Name:	Error2.jpg 
Views:	27 
Size:	19.1 KB 
ID:	50501

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Well copy what you need to test to a new DB and see if it works then?
    Failing that you mght need to upload enough for us to try and test ourselves.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    69
    I have copied to a new DB with the same results. I also have repaired my Office 365 apps, still the same.

    The DB is structured as follows (not sure if I can upload the demo DB for you to use to test).

    1. A table with 2 fields (MyMonth as Date/Time and Quantity as number field)
    2. Query that shows the above 2 fields
    3. Two expression fields in the query that checks the following:
    i) If the Quantity field = 1 then display "Yes" else display "No"
    ii) If the Quantity minus 3 (Quantity - 3) = 1 then display "1" else display "0"

    I have done these types of expressions with IIF in queries in other databases with no issues. This is the first time that I am encountering this.

    Thanks

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I will not be able to touch a 365 DB, so I am out.

    See attachments on your reply for uploading DB.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    The syntax looks correct, so either the quotes aren't normal double quotes (It's difficult to tell in that font) or your regional settings separators shouldn't be comma's .

    If all of the above isn't the cause you'll have to upload a sample demonstrating the issue.
    Remove any sensitive data, compact and repair, then zip the file and attach it here.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  11. #11
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    69
    Hi,
    I have changed the Iif statement and is now giving me the desired results. It seems to be the data type of the field that causes the error. I attach the solved expression..

    Click image for larger version. 

Name:	Solved.jpg 
Views:	20 
Size:	19.3 KB 
ID:	50502

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You are telling us quantity is text?
    If you are using Val() to convert to a numeric, why are you comparing against text?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    69
    The Quantity field is NUMBER. I attach the demo DB, maybe it will help.

    Database2.zip

  14. #14
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    your number is a double and doubles are not precise. Assuming quantity cannot be fractional (e.g. 1.5) then change the number type to integer or long. If you need decimal precision use the currency datatype. Or use the round function instead of val

  15. #15
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    69
    Hi CJ,
    I changed the number type to Integer, Long and Decimal, but with the same results!

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. SQL Query using IF Statement???
    By bishmedia in forum Queries
    Replies: 13
    Last Post: 05-23-2018, 12:53 PM
  2. Replies: 8
    Last Post: 05-08-2018, 07:27 AM
  3. WHERE statement (between) in a query
    By bignate in forum Queries
    Replies: 2
    Last Post: 09-12-2013, 04:51 AM
  4. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  5. Replies: 3
    Last Post: 07-10-2012, 05:23 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