Results 1 to 4 of 4

Thread: Single Quotes

  1. #1
    Join Date
    Mar 2004
    Location
    Atlanta, GA
    Posts
    58

    Unanswered: Single Quotes

    I am having problems because my strQuery can include single quotes. What would be the best way to handle this?

    Code:
    strSQL = "INSERT INTO tblSnapshot ([user], [date], [description], " & _
             "[fields], [query]) " & _
             "VALUES ('" & strUser & "', '" & strDate & "', '" " & _
             "& strDescription & "', '" & strSelect & "', '" & strQuery & "')"

  2. #2
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1
    HI

    I assume that all the fields are text AND you want to include the single quotes in the field entries.

    If so, then what is the problem?

    MTB

  3. #3
    Join Date
    Oct 2003
    Location
    Ger
    Posts
    1,969
    Provided Answers: 1
    You can use a Constant or a Variable containing the value of Chr$(39) Which is the single Quote

  4. #4
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    You can replace your single quotes with double quotes like this:

    Code:
    strSQL = "INSERT INTO tblSnapshot ([user], [date], [description], " & _
             "[fields], [query]) " & _
             "VALUES ('" & strUser & "', '" & strDate & "', '" " & _
             "& strDescription & "', '" & strSelect & "', """ & strQuery & """)"
    To get a double quote within double quotes you need four double quotes. Ex. """" is a literal ". Any two double quotes together between double quotes is interpreted as one double quote. Another example is "This is a "" test." Would be translated as This is a " Test. The problem with the double quotes is it gets hard to read unless you are used to it. Another approach would be to use Chr(34) in place of the "".

    Code:
    strSQL = "INSERT INTO tblSnapshot ([user], [date], [description], " & _
             "[fields], [query]) " & _
             "VALUES ('" & strUser & "', '" & strDate & "', '" " & _
             "& strDescription & "', '" & strSelect & "', " & Chr(34) & strQuery & Chr(34) & ")"

Posting Permissions

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