Results 1 to 13 of 13
  1. #1
    Redchrome is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2022
    Posts
    11

    Names with Apostrophes

    Not sure if this is the right sub-forum, but I have a issue with 3 employees who have apostrophes in their names.



    I have a table for "Employee List" with a field for last name.
    I have a 2022 Uniform Order Table with a matching last name field.
    I have an query to merge last first and middle name driven by this "FullName: [LastName] & ", " & [FirstName] & " " & [MiddleInitial] & [Suffix]"
    The form to write to the table uses this as its Row Source "SELECT [2022EmployeeNameQuery].FullName, [2022EmployeeNameQuery].[Employee ID] FROM 2022EmployeeNameQuery; "

    When selecting the name from the form its not actually writing to the record it should. I know it's an issue with the apostrophes (As they are the only 3 of 219 employees with an issue)

    I am "self"-taught in access via trial-and-error mixed with forums and YouTube. How messed up did I make myself, and what can I do to fix this. Thanks in advance for all your help.

    --RC

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    The issue you are having is well known. To resolve the issue you have to "escape the quote".

    The simplest method to escape single quotes in SQL is to use two single quotes. For example, if you wanted to show the value O’Reilly, you would use two quotes in the middle instead of one.

    Code:
    SELECT 'O''Reilly' AS.......

  3. #3
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    pick your poison,
    you can either use single quotes, or the double, but not both.
    I allow single qutoes only, before running query. I use dbl-quotes to define the strings.
    I fix the field via:

    replace([field],chr(34),"'")

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    You can also use Replace() and replace the single quote with two single quotes I believe?
    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
    Redchrome is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2022
    Posts
    11
    Orange, maybe I'm not grasping where since the apostrophe is in the middle of the field, and not concatenated from an "O" field and a "Brien" field. The SQL for the query that merges name and drive the combo box is this.

    SELECT EmployeeListTable.[Employee ID], [LastName] & ", " & [FirstName] & " " & [MiddleInitial] & [Suffix] AS FullName
    FROM EmployeeListTable
    ORDER BY [LastName] & ", " & [FirstName] & " " & [MiddleInitial] & [Suffix];

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725

  7. #7
    Redchrome is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2022
    Posts
    11
    Cropped to remove as much personal data as I could and still give what issue is. As seen the apostrophe is part of the data in the name in the field. Thanks again for any help from all comments so far, I am very very green with this, some of the comments i have no clue what they even mean, but appreciate it anyway.

    Edit: Not sure why picture didn't upload, trying again.

    Edit: Still not posting, may be work security block. Will try from home later.

  8. #8
    Redchrome is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2022
    Posts
    11

  9. #9
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    name'withquoteinmiddle could be handled using name''withquoteinmiddle

    1 single quote gets replaced with 2 single quotes

  10. #10
    Redchrome is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2022
    Posts
    11
    So in the actual last name field I would enter their name as O''Brien (two singles quotes not a double) for example instead of O'Brien?

  11. #11
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    That's the result you need in order to process the apostrophe.
    As Paul said, you could use

    Replace("name'withquote", "'","''")

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    No, as that is what you would see all the time, and that is not their name.
    You use the replace or whatever, when you are manipulating the names.
    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
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I have a function is a standard module
    Code:
    Function fConvertQuotesSingle(InputVal)
       fConvertQuotesSingle = Replace(InputVal, "'", "''")
    End Function

    Usage is:
    Code:
    & fConvertQuotesSingle(vLast) & ", " & fConvertQuotesSingle(vFirst) & ", " &
    or

    Code:
    & "', '" & Nz(fConvertQuotesSingle(r.Fields("LName")), "")
    There are from Queries in code....

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

Similar Threads

  1. Handleing apostrophes
    By JimO in forum Access
    Replies: 2
    Last Post: 08-15-2019, 10:32 AM
  2. searching for names with apostrophes
    By kwelch in forum Programming
    Replies: 4
    Last Post: 01-12-2017, 11:23 AM
  3. sql variable with apostrophes
    By broecher in forum Programming
    Replies: 3
    Last Post: 04-23-2013, 11:00 AM
  4. Replies: 2
    Last Post: 04-17-2013, 04:59 AM
  5. Replies: 4
    Last Post: 09-05-2011, 11:10 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