Results 1 to 7 of 7
  1. #1
    deekadelic is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jun 2016
    Posts
    23

    How to Filter For Criteria With Single Quotes in the String

    I'm trying to write a query that will generate a field from a table where the value in field "CName" contains either "Bills" or "Bill's", but I'm getting wacky results, for instance: records where neither "Bill", "Bills", "Bill's", nor any other value containing a single quote is contained in the field in question. I've narrowed the query down through enough trial and error to know that it is this value of "Bill's" that is causing the problem. But I don't know what syntax the query is looking for to identifty those records, nor how this syntax error could be resulting in some of the records I'm seeing.



    Here's the query code for this field:

    Customer_NEWEST: IIf([tbl_CUSTOMER_AD_HOC_REPORT]![CName] Like "*Bill's*" Or "*Bills*","BILLS","OTHER")

    Please advise. Thank you

    - Deek

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Should "Bill" be included?
    If so, then just look for "Bill", as that would be included in all those different options.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The short answer is you have to double up on the single quote.

    Customer_NEWEST: IIf([tbl_CUSTOMER_AD_HOC_REPORT]![CName] Like "*Bill''s*" Or "*Bills*","BILLS","OTHER")




    -----------------------------------------------------------
    I have a function that I use in queries to automatically double the single quote in a word.
    Code:
    Function ConvertQuotesSingle(InputVal)
       ConvertQuotesSingle = Replace(InputVal, "'", "''")
    End Function

  4. #4
    deekadelic is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jun 2016
    Posts
    23
    The thing with "Bill" is that there are other values such as "Bill Gunderson" that I do not want this query to capture.

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    The condition part of your Iif is incorrect. You can't use something like "If x = 1 or 2". We humans can make sense out of it, but Access can't - you need to say if X = 1 OR X = 2, so your statement has to be:

    Customer_NEWEST: IIf([tbl_CUSTOMER_AD_HOC_REPORT]![CName] Like "*Bill's*" Or [tbl_CUSTOMER_AD_HOC_REPORT]![CName] Like "*Bills*","BILLS","OTHER")

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Oops, I missed that.

    Good catch John_G.

  7. #7
    deekadelic is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jun 2016
    Posts
    23
    That did the trick!

    Thank you!

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

Similar Threads

  1. String without the quotes
    By tmcrouse in forum Access
    Replies: 3
    Last Post: 01-20-2016, 02:11 PM
  2. need help with single and double quotes
    By vicsaccess in forum Programming
    Replies: 7
    Last Post: 12-27-2015, 06:40 PM
  3. Single and double quotes in a INSERT statement
    By dccjr in forum Programming
    Replies: 16
    Last Post: 03-14-2013, 09:50 PM
  4. Suppress quotes when writing a string via Write #
    By GraeagleBill in forum Programming
    Replies: 9
    Last Post: 10-08-2012, 06:29 PM
  5. Replies: 1
    Last Post: 12-04-2011, 06:33 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