CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Guest

    Single Quote Problem in SQL statement

    Hi,
    My program has a listbox, which lists all the product names. Some of the products names contain one or more single quotes ('). The problem is the following. Please look at the code...

    =====================================
    Dim db as Database
    Dim rs as Recordset
    Set db =OpenDatabase(App.Path & "/mydb.mdb")
    Set rs= db.OpenRecordset("SELECT * FROM MainTable Where [ItemName]='" _
    & lstItems.text & "'" , dbOpenDynaset) 'Where lstItems is the listbox control
    ========================================

    lstItems has the product name of "American's Flag 4'x8'". When I put it into the SQL statement, it returns a Syntex error because the single quotes ('). Is there anyone knows how to solve the problem? In C, I know that I could reformat the string as
    Str="American\'s Flag 4\'x8\'"

    How can I do the same thing in VB?

    Thank You.



  2. #2
    Join Date
    Jun 2000
    Posts
    56

    Re: Single Quote Problem in SQL statement

    use
    Select * from Customers Where CustomerID=" & chr(34) & lstItems.text & Chr(34)
    Good Luck


  3. #3
    Guest

    Re: Single Quote Problem in SQL statement

    You can write a Code that will pad a Quote to the string like
    McDonnald's you need string like 'MCDonnald''s'. you can use CHAR(34), but what if you have more quotes in string like ->> PS'tsu'sd'd.

    So my friend the best solution is write a routine that will Pad a string with

    quote.

    enjoy,




  4. #4
    Join Date
    Apr 1999
    Location
    Rotterdam, Netherlands
    Posts
    278

    Re: Single Quote Problem in SQL statement

    With VB6 you can use the Replace$ function.
    Eg.
    sToSearch = Replace$(sToSearch, "'", "''")

    Crazy D :-)
    "One ring rules them all"

  5. #5
    Join Date
    Jul 1999
    Posts
    104

    Re: Single Quote Problem in SQL statement

    You can add an extra quote (works for oracle)
    I always use the following funcion


    public Function schoonmakenString(naam as string) as string
    Dim teller as Integer

    teller = 1
    While teller <= len(naam)
    If mid(naam, teller, 1) = "'" then
    'if the char is an "'" then put an second "'"
    'after it (just include the "'" two times
    'in the mid funtion
    naam = mid(naam, 1, teller) & mid(naam, teller, len(naam))
    teller = teller + 1
    End If
    teller = teller + 1
    Wend
    schoonmakenString = naam
    End Function





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