Results 1 to 8 of 8
  1. #1
    hansdbase is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    Nov 2022
    Posts
    30

    The dreaded apostrophe

    I have a form with a text field and a command button. When text is entered into the field and the command button is pressed it adds the text to the table to the appropriate field. But if the text contains an apostrophe, I of course get an error. I put the text variable in [] but it still gives me an error. Is there any easy way around this? 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,895
    Why do you get error? Are you running SQL in VBA? [] would not be appropriate for delimiting a VBA variable.

    Why would you need any code for this data entry? Are you not using bound form?
    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
    hansdbase is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    Nov 2022
    Posts
    30
    It is not set up as a bound form. It is a vba sql command. Is there any way around this apostrophe dilemma in such a case?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    Options:

    1. use Replace() function to escape apostrophe, like:
    Code:
    CurrentDb.Execute "INSERT INTO table(field) VALUES(" & Replace(textbox, "'", "''") & ")"
    2. use recordset and its AddNew and Update methods then apostrophe doesn't matter
    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
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    shouldn't that be?

    CurrentDb.Execute "INSERT INTO table(field) VALUES('" & Replace(textbox, "'", "''") & "')"

  6. #6
    hansdbase is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    Nov 2022
    Posts
    30
    I am curious about your question. Shouldn't what be? Thank you to June and you for the Replace function

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    I am curious about your question. Shouldn't what be?
    VALUES(" & Replace(textbox, "'", "''") & ")"needs the single quotes
    VALUES
    ('" & Replace(textbox, "'", "''") & "')"

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    Good point. Since this is a text field, yes, apostrophe (single quote) delimiters. However, # if field is date/time, nothing for number type and of course no Replace() for these data types.
    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.

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

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2021, 02:12 PM
  2. The dreaded split
    By DevState in forum Queries
    Replies: 20
    Last Post: 08-01-2018, 07:38 AM
  3. Replies: 3
    Last Post: 07-30-2018, 05:45 PM
  4. Issues using apostrophe
    By rosscortb in forum Access
    Replies: 5
    Last Post: 02-05-2015, 11:34 AM
  5. Apostrophe in name
    By NISMOJim in forum Programming
    Replies: 1
    Last Post: 04-04-2013, 10:14 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