Results 1 to 4 of 4

Thread: [RESOLVED] SQL Search with ' in the String Variable

  1. #1

    Thread Starter
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Resolved [RESOLVED] SQL Search with ' in the String Variable

    Hello All,

    I have a program that displays / edits the company and contact details of our customers.

    I have a simple SQL line that retreives all the contacts at a specific company when that company is selected.

    Code:
    ADOContacts.RecordSource = "Select [SURNAME], [FIRST NAME], [COMPANY], [CONTACT ID] FROM Contacts WHERE Contacts.COMPANY='" & LblCompanyName.Caption & "' ORDER BY [SURNAME]"
    I have now found a limitation I didn't think about where we have a customer with an apostrophe in it's name. EG. Mary's Company.

    So when the SQL query gets sent with this customer selected I receive an error because of the extra apostrophe.

    Code:
    "Select [SURNAME], [FIRST NAME], [COMPANY], [CONTACT ID] FROM Contacts WHERE Contacts.COMPANY='Mary's Company' ORDER BY [SURNAME]"
    It thinks the company name is 'MARY' and wonders why the s Company' is there.

    Is there a way to avoid this error or do i just need to not allow aphostrophes in the Company Name field?

    Thanks for your help,

    Cheers,

    Hojo.
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: SQL Search with ' in the String Variable

    Handle apostrophes with a Replace() call:
    Code:
    ADOContacts.RecordSource = "Select [SURNAME], [FIRST NAME], [COMPANY], " & _
            "[CONTACT ID] FROM Contacts WHERE Contacts.COMPANY='" & _
            Replace$(LblCompanyName.Caption, "'", "''") & "' ORDER BY [SURNAME]"
    You want to use double apostrophes in this case
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: SQL Search with ' in the String Variable

    Thanks LaVolpe.

    Works a treat.

    I love simple solutions.

    Thanks again,

    Cheers,

    Hojo
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] SQL Search with ' in the String Variable

    Thread moved to 'Database Development' forum (the 'VB6' forum is only meant for questions which don't fit in more specific forums)

    While it may seem that the issue is solved, there are many more waiting for you - especially if somebody malicious uses your program.

    For further explanation (and links to code examples), see the article Why should I use Parameters instead of putting values into my SQL string? from our Database Development FAQs/Tutorials (at the top of this forum).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width