Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single quote in query string causing problems

Status
Not open for further replies.
Jan 22, 2001
124
US
Here is the code:

CurrentDb.Execute "UPDATE table SET table.COMMENTS =" & "'" & Me![ReqComments] & "'" & " WHERE table.ID = 2;"

The problem comes about when the Me![ReqComments] memo field contains a single quote (example: John's book).

Any help with this would be greatly appreciated. Thanks in advance.

--Rob
 
Wrap the value in a pair of double quotes:

& """" & Me![ReqComments] & """"

< M!ke >
 
Rob,
You could use the Replace function:

"UPDATE table SET table.COMMENTS =" & "'" & Replace(Me![ReqComments],"'","") & "'" & " WHERE table.ID = 2;"

You might have to do it in a string outside the SQL if MS has trouble parsing the statement.

Tranman
 
Thanks Mike! That solves the problem if the user enters a comment that includes a single quote.

Now, if a user enters a comment with a double quote, I get an error.

How can I allow BOTH of these characters?
 
If you use single quotes as the text delimiter, use the following:

Code:
    Dim strSQL  As String
    Dim strComment As String
    
    strComment = Replace(txtComment, "'", "''")
     
    strSQL = "Update tblComments Set Comment = '" & strComment & "' WHERE key = 'a';"
    
    CurrentDb.Execute strSQL

If you use double quotes as the text delimiter, use the following:

Code:
    Dim strSQL  As String
    Dim strComment As String
    
    strComment = Replace(txtComment, """", """""")
     
    strSQL = "Update tblComments Set Comment = """ & strComment & """ WHERE key = 'a';"
    
    CurrentDb.Execute strSQL


 
In thread705-653379 I posted a function called Jetstream that copes with " and ' embedded in your SQL string.

Copy it into a module and change your SQL to
Code:
CurrentDb.Execute "UPDATE table SET table.COMMENTS =" & JetStream(Me![ReqComments]) & " WHERE table.ID = 2;"

hth

Ben

----------------------------------------------
Ben O'Hara &quot;Where are all the stupid people from...
...And how'd they get so dumb?&quot;
rockband.gif
NoFX-The Decline
----------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top