[Webinar] Streamline your web hosting managementRegister Today


Apostrophe Error in SQL matching

Posted on 2004-08-11
Medium Priority
Last Modified: 2013-12-25
I have used code similar to the following to search for matching records in a database, and it works well, providing a narrowing list of matches as the 'Search' texts gets longer.

Variables FtExt1 and FtExt2 are the contents of the two Search text boxes, and the code is called to update the Recordset after every change in either text box.

The problem occurs when the Search text includes an APOSTROPHE eg O'Carrol. This is because SQL uses the apostrophe for its' own purposes, and this extra apostrophe destroys the logic of the SQL.

If the user types an Apostrophe, the program crashed, until I added code to trap this character

    SQL = "Select [ID], [Title], [Name], Age FROM Entrants WHERE ([Title] like '" & FtExt1 & "' OR [Title] is Null) AND ([Name] like '" & FtExt2 & "') ORDER BY 3,2;"
    MatchingRs.Open SQL, SMConn, adOpenKeyset, adLockReadOnly

This problem is not a large one, but does restrict the scope of my 'Find' routine to some extent.

Is there a way to include the apostrophe in the SQL without crashing it


Question by:nedwob
LVL 71

Assisted Solution

by:Éric Moreau
Éric Moreau earned 120 total points
ID: 11774001
You have to double it:

 SQL = "Select [ID], [Title], [Name], Age FROM Entrants WHERE ([Title] like '" & replace(FtExt1, "'","''") & "' OR [Title] is Null) AND ([Name] like '" & replace(FtExt2,"'", "''") & "') ORDER BY 3,2;"

Assisted Solution

bkthompson2112 earned 120 total points
ID: 11774007
Hi nedwob,

Use 2 apostrophes.  ''
When the user enters an apostrophe add another to the search string.

LVL 29

Accepted Solution

leonstryker earned 320 total points
ID: 11774575
You may want to use a function to do this to all of your SQl string before passing them to the database.  Here is sample function which does it:


Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


Assisted Solution

PePi earned 200 total points
ID: 11774804
use replace like so:

SQL = "Select [ID], [Title], [Name], Age FROM Entrants WHERE ([Title] like '" & Replace(FtExt1,"'","''") & "' OR [Title] is Null) AND ([Name] like '" & Replace(FtExt2,"'","''") & "') ORDER BY 3,2;"

in the replace function, the second parameter is a double quote, single quote, double quote
the third parameter is a double quote,  a single quote, another single quote then a double quote.

it's very hard to see the difference of these single & double quotes just by looking at it. hope this helps

Assisted Solution

mladenovicz earned 280 total points
ID: 11781918
Public Function m_PrepareQueryParams(str As String) As String
Dim sRes As String
    sRes = str
    sRes = Replace(sRes, "[", "[[]")
    sRes = Replace(sRes, "*", "[*]")
    sRes = Replace(sRes, "?", "[?]")
    sRes = Replace(sRes, "#", "[#]")
    sRes = Replace(sRes, "%", "[%]")
    sRes = Replace(sRes, "_", "[_]")
    sRes = Replace(sRes, "'", "''")
    m_PrepareQueryParams = sRes
End Function

Author Comment

ID: 11784958
Thanks for the comments. I will look at them and come back to award the points in a day or two.

LVL 18

Assisted Solution

JR2003 earned 160 total points
ID: 11826298
I use a function called DBStr and call it with every string I put into some sql.
Just paste the function into a module and call it from all the literals you put in sql.
It works a treat...

'Example Usage:

 SQL = "Select [ID], [Title], [Name], Age FROM Entrants WHERE ([Title] like '" & DBStr(FtExt1) & "' OR [Title] is Null) AND ([Name] like '" & DBStr(FtExt2) & "') ORDER BY 3,2;"


Public Function DBStr(sIn As String) As String

    Dim tmp As String
    Dim i As Long
    Dim j As Long
    If Len(sIn) <> 0 Then
        j = 1
            i = InStr(j, sIn, "'")
            If i = 0 Then Exit Do
            tmp = tmp & Mid$(sIn, j, i - j) & "''"
            j = i + 1
        DBStr = tmp & Mid(sIn, j)
    End If
End Function


Author Comment

ID: 11831908
Thanks to all. I have increased the points and will split according to the input or usefulness to me.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 12 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question