Results 1 to 7 of 7
  1. #1
    eseng is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2022
    Posts
    3

    Syntax error 3075 when typing name with apostrophe in form

    I am using this vba code to prevent double entry:
    Private Sub BusinessName_AfterUpdate()
    Dim NewBusinessName As String


    Dim stlinkcriteria As String


    NewBusinessName = Me.BusinessName.Value
    stlinkcriteria = "[BusinessName] = " & "'" & NewBusinessName & "'"
    If Me.BusinessName = DLookup("[BusinessName]", "Sheet1", stlinkcriteria) Then
    MsgBox "" & NewBusinessName & " is already in the Database." _
    & vbCr & vbCr & "Data Entry Denied!!!", vbInformation, "DUPLICATE ENTRY"
    Me.Undo 'undo the process and clear all fields

    The code works well to prevent double entry, unfortunately, if the name I typed includes an APOSTROPHE (like John's Cafe), syntax error 3075 appears.
    Do you have any suggestions to correct the error?

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Try

    Code:
    stlinkcriteria = "[BusinessName] = " & chr(34) & NewBusinessName & chr(34)

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Those apostrophes will likely cause you problems from here on in - especially in code. For queries, one work around is to have a field with and a field without. Your criteria could be against the without field but all user need to know not to use them. Your forms/reports would show the other field. If you must use them then in code try concatenating with the ASCII character equivalent:

    "John" & chr(39) & "s" for John's
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    use the replace function to replace a single quote with two single quotes

    stlinkcriteria = "[BusinessName] = '" & replace(NewBusinessName,"'","''") & "'"



  5. #5
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I had problems writing append and update queries when data had embedded single quotes.
    Like Ajax, I used the replace function, but I wrote my own function

    Code:
    Public Function fnConvertQuotesSingle(InputVal)
       fnConvertQuotesSingle = Replace(InputVal, "'", "''")
    End Function

    Usage would be:
    Code:
    stlinkcriteria = "[BusinessName] = '" & fnConvertQuotesSingle(NewBusinessName) & "'"

  6. #6
    eseng is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2022
    Posts
    3
    Quote Originally Posted by davegri View Post
    Try

    Code:
    stlinkcriteria = "[BusinessName] = " & chr(34) & NewBusinessName & chr(34)
    It worked! Thanks sir

  7. #7
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Glad to help. I find this method SO much simpler than the confusing (single?, double?) quotes within quotes within more quotes.

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

Similar Threads

  1. Runtime Error 3075: Apostrophe Throws an Error
    By besuchanko in forum Modules
    Replies: 3
    Last Post: 03-15-2022, 05:21 PM
  2. Replies: 6
    Last Post: 02-03-2020, 07:09 PM
  3. Syntax Error when using Apostrophe
    By PicoTTS in forum Access
    Replies: 4
    Last Post: 06-27-2017, 07:26 PM
  4. Replies: 5
    Last Post: 09-05-2012, 09:28 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