handling glitches in sql string

Posted on 2006-05-01
Last Modified: 2010-05-01
I need to include glitches within a query string, but keep getting an "unterminated string" error.  How would I modify the below to accept glitches within the Name field?

Set conn = CreateObject("ADODB.Connection")
conn.Open MSSqlConnStr

MyName = "O'Brien"

MySql = "SELECT * FROM MyTable WHERE Name = '" & MyName & "'"

Set rs = conn.Execute(MySql)

If Not rs.EOF Then MsgBox("Found Name!")

Question by:arichexe
    1 Comment
    LVL 9

    Accepted Solution

    Is your back end Sql Server?  From the documentation:

    When using single quotation marks to delimit a character constant that
    contains an embedded single quotation mark, use two single quotation marks
    to represent the embedded single quotation mark.

    For example,
    MyName= replace(MyName, "'", "''")


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    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…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now