How To Escape Apostrophe In SQL Update Query

If you are looping a table with thousands of records, you’ll probably run into at least one that has an apostrophe in the field name.

Like “Mike’s” or “M’cormick”, or something else.

Anyway, here is one way to escape the string when you are doing your update query.


Option Compare Database

Sub YDriveLoop()
    '4/23/24 erik@loeblcomservices.com
    
    Dim rst As dao.Recordset
    Dim strTitle As String, strBatesNo As String
    Dim strUpdateSQL As String
    
    Set rst = CurrentDb.OpenRecordset("SELECT BegBates,[Folder/File],Title FROM YDrive")
    
    strTitle = "blank"
    
    Do Until rst.EOF
        strBatesNo = rst.Fields("BegBates")
        If rst.Fields("Folder/File") = "Folder" Then
            strTitle = rst.Fields("Title")
        End If
        
        
        strUpdateSQL = "UPDATE YDrive SET DrivePath= '" & SQLFixup(strTitle) & "' WHERE BegBates='" & strBatesNo & "'"
        
        CurrentDb.Execute (strUpdateSQL)
        
        Debug.Print "updating: " & strBatesNo
        rst.MoveNext
    Loop
    
    rst.Close
    Set rst = Nothing
    
End Sub

Function ReplaceStr(TextIn, ByVal SearchStr As String, _
    ByVal Replacement As String, _
    ByVal CompMode As Integer)

    Dim WorkText As String, Pointer As Integer
    
    If IsNull(TextIn) Then
        ReplaceStr = Null
    Else
        WorkText = TextIn
        Pointer = InStr(1, WorkText, SearchStr, CompMode)
        Do While Pointer <> 0
            WorkText = Left(WorkText, Pointer - 1) & Replacement & _
            Mid(WorkText, Pointer + Len(SearchStr))
            Pointer = InStr(Pointer + Len(Replacement), WorkText, _
            SearchStr, CompMode)
        Loop
        ReplaceStr = WorkText
    End If

End Function

Function SQLFixup(TextIn)

    SQLFixup = ReplaceStr(TextIn, "'", "''", 0)

End Function


Hope it helps.




By the way, if you got or are getting value from the VBA information, please click the "Donate" button to give me a small token of your appreciation, thanks!


These posts may help answer your question too...

Learn Access VBA: Understand Tables, Queries, Forms, and Reports

Learn Access VBA: From Zero to Database Hero If you’ve ever opened Microsoft Access and wondered how all the pieces fit together — tables, queries, forms, and reports — this tutorial is made for you. In just a few minutes, you’ll understand how Access works behind the scenes and see how VBA (Visual Basic for […]

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Create A Parameter Query In Access

A parameter query changes your ordinary static access query to be more dynamic and interactive. It will ask you a question about what you want to search for, allowing you to do a search query multiple times instead of just once. You can do your parameter query straight from the QBE (Query By Example) Editor, […]


Support these sponsors:
Tags: ,
Previous Post
Error 91 Flowchart vbahowto-com
Errors Excel related

Error 91: Object Variable or With Block Variable Not Set — Causes and Solutions

Next Post

How To Make An Access Form Time Picker

Leave a Reply

Your email address will not be published. Required fields are marked *