CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2001
    Location
    Belgium
    Posts
    39

    Special Characters on SQL-statement

    I have the following:


    Dim SQL as string
    ...
    SQL = "SELECT * FROM COMPANY WHERE NAME = '" & txtName.text &"'"
    ...
    rstCompany = conOracle.OpenRecordset(SQL, dbOpenForwardOnly)




    Works fine for my purpose until I encounter name with the '-character, for example d'Or. My ODBC call then fails because it assumes that I'm only looking for NAME = 'd' and that the Or' at the end is another part of the SQL statement that cannot be parsed.

    Any solution or workaround GREATLY appreciated (and rated ;-)

    Thanks

    <i>If anything in this post makes sense it was written by me, if not I don't know who wrote it</i>

  2. #2
    Join Date
    Aug 2000
    Location
    England
    Posts
    185

    Re: Special Characters on SQL-statement

    Use two single qoutes instead of one, i.e

    d''or

    That should fix it!

    Andrew


  3. #3
    Join Date
    Jul 2001
    Location
    Belgium
    Posts
    39

    Re: Special Characters on SQL-statement

    Sounds tempting but I still have one problem, the text isn't always coming from a textbox (I only use it to test). The real names come from MS Access and dBase and there are already '-characters in there. Since there are a couple hundred of these external db's I'm not really planning to change them manually.
    The most functional solution would be a procedure that can replace the ' with something else (or just remove it). Normally I program for VC++ and I don't know of any stnadard VB function to replace characters in a String.

    If anything in this post makes sense it was written by me, if not I don't know who wrote it

  4. #4
    Join Date
    Jun 2001
    Location
    Northeast Tennessee
    Posts
    15

    Re: Special Characters on SQL-statement

    ... and this will make the replacement for you:

    SQL = Replace (SQL,"'","''",1,-1,vbTextCompare)


  5. #5
    Join Date
    Aug 2000
    Location
    England
    Posts
    185

    Re: Special Characters on SQL-statement

    try this



    dim counter as integer

    for counter = len(myStr) to 1 step -1
    if mid (myStr, counter, 1) = "'") then
    mid(myStr, counter, 1) = ""
    end if
    next counter





    This will remove them from the string


  6. #6
    Join Date
    Aug 2001
    Location
    PA
    Posts
    150

    Re: Special Characters on SQL-statement

    Hi,

    SQL = "SELECT * FROM COMPANY WHERE NAME = '" & chr(39) & txtName.text & chr(39) &"'"

    Try this one. I had a problem like this before and when I put my variable within chr(39) which is a single qoute it worked.

    Thanks
    Hisham
    Thank You, Hisham

  7. #7
    Join Date
    Aug 2001
    Location
    PA
    Posts
    150

    Re: Special Characters on SQL-statement

    Access DB when it has names that contain ' in them tends not to find them if you search for them. Unless, you put the entire name withing single qoutes.

    Example:

    Job'todo

    if you search for it it won't find it.

    Solution:

    'Job'todo'

    This will work. In VB to to put the single qoutes use chr(39).
    The solution for your SQL is

    txtName.text replace with
    chr(39) & txtName.text & chr(39)

    chr(39) ---------> '

    Thanks
    Hisham
    Thank You, Hisham

  8. #8
    Join Date
    Aug 2001
    Location
    PA
    Posts
    150

    Re: Special Characters on SQL-statement

    By the way, having single quotes around a variable won't affect it if it does not have a single qoute within it.

    'cool' you still can search it with r without the single qoutes. So, having single qoutes won't affect your other work

    Thanks
    Hisham
    Thank You, Hisham

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

Featured