Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    CP611 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    55

    IIF Statements in a Query - Syntax Error Message


    I'm using an Append Query and want to change if it says F to Female and M to Male. My current formula says: IIf([Gender]="F","Female","Male") but when I try to run the query it comes up with the error message: Syntax error (comma) in query expression. I've tried putting a ; instead of a , as someone else suggested but still doesn't work. Any ideas?! It's driving me crazy! (I am a complete novice when it comes to Access) thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    An APPEND query adds records, an UPDATE changes values.

    Post your complete SQL statement.
    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
    CP611 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    55
    I really don't know much about Access, I'm trying to teach myself! I'm basically inputting data onto one table and using an append query to add it to another table but in a different order and with concatenations. So for example in my input table I've got two columns Address line 1 and Address line 2 and using the append query I've bought them both together into one column but with a Pipe added using this Expression: [Address] & "|" & [Address2]. I thought using the IIF statement in a similar way I could append the output table to change F to Female. Is this the right way to do it?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    What you are doing is not conventional. Instead of saving to another table, should just do those calcs in a SELECT query.

    Have you completed an introductory tutorial book?
    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.

  5. #5
    CP611 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    55
    I did look at the select query but the problem that I had with this is in second table (that I am currently appending to) has extra columns that weren't in the first table. Is there a way with a select query that this can be done?

    No I haven't completed a introductory tutorial book, I've just been using Google!

  6. #6
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    To agree with what June7 said, query would seem to be the way to go. Queries are the power of databases like MS Access. Gives you a lot of flexibility to manipulate the data without having to change the underlying data in the table. I really don't see anything wrong with your IIf statement. Could be issue with the field in 2nd table you are trying to append it to? Is that gender field in 2nd table a text field? And yes you can have other fields not in the table in a Select query.

  7. #7
    CP611 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    55
    Yes the Gender field in the 2nd table I'm trying to append to is a text field. How would I go back adding fields into a select query that aren't in the original table that I'm selecting from? I've tried to do it before and it didn't come out with any data

  8. #8
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    These new fields not tied to a table are just variables so if you wanted to add the current date, in a column you would put CurrDate: Date() or if you wanted a field to combine 2 other fields would be like FullName: [FirstName] & " " & [LastName]. So you put the name you want to call it, then a colon, space and then what you want the field to hold.

  9. #9
    CP611 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    55
    Ok great I'll give that a go. Some of these new fields will not have any data in them, is that an issue? Because basically I want the table created from the select query to then be exported into excel and then the data is imported onto our database using an import tool which has a ready made spreadsheet which we just copy and paste data on to and import. Therefore some of the fields brought out from the select query don't contain any data (as they are filled in our database) but we will need the new headers to be there in the correct order. Thanks for your help so far

  10. #10
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    You should just be able to do one of the following:
    NewField: ""
    NewField: " "
    NewFIeld: null

  11. #11
    CP611 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    55
    Thank you for your help I'll give that a go and see what happens!

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Query can be exported to Excel. So far, nothing really justifies second table.

    You want to manipulate the data, export to Excel, then import into a different db?
    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.

  13. #13
    Geo21 is offline Novice
    Windows 8 Access 2013
    Join Date
    May 2014
    Posts
    20

    IIF Statements in a Query - Syntax Error Message

    Could you post the whole query?

  14. #14
    CP611 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    55
    I've managed to create what I want from just using a select query, however I am still getting the same error message when I put in the IIF statement of: IIf([Gender]="F","Female","Male") of Syntax error (comma) in query expression. Any ideas?!

  15. #15
    CP611 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    55
    This is the SQL for the query (Table 1 is just the table I'm selecting from - as you can see I also have quite a few new fields where I just need the header hence the null).
    SELECT Null AS SERIALNUMBER, Null AS CONTACTTYPE, Null AS TITLE, [Table1].[First Name], [Table1].[Last Name], [Table1].Address & "|" & [Address2] AS [=Expr], [Table1].[Suburb/City], [Table1].County, [Table1].[Postal Code], [Table1].Country, IIf([Gender]="F","Female","Male") AS Expr
    FROM Table1;

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

Similar Threads

  1. Error with Query based on IIF statements
    By tylerpickering in forum Queries
    Replies: 2
    Last Post: 06-17-2014, 02:36 PM
  2. Replies: 11
    Last Post: 05-01-2014, 11:56 AM
  3. Mysterious date syntax error message
    By Monterey_Manzer in forum Queries
    Replies: 12
    Last Post: 12-23-2013, 06:56 PM
  4. Replies: 3
    Last Post: 03-05-2013, 11:17 AM
  5. Replies: 6
    Last Post: 05-30-2012, 12:32 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