I was just testing my database and I realized that I run into problems wherever a text entry in my database contains a '
character (single quote). My solution for now is that before any .execute operations on a string, I call escape(string, "'", " "'" ")
.
Summarized example below:
qr = "INSERT INTO tblExample VALUES ( " & "'" & me.testparam & "'" & ");"
qr = Replace(qr, "'", " "'" ")
db.execute qr
'also tried qr = "INSERT INTO tblExample VALUES ( " & "'" & replace(me.testparam,"'"," ") & "'" & ");"
This was what I assumed to be the correct workaround to prevent errors from values such as Tourette's
.
There's two problems with this. First of all, it's not working. Second, I have over 50 locations in code throughout my app where I call the statement db.execute qr where qr is a string that could potentially contain a single quote. I need the field in the table to contain the single quote, so I can't just replace it with a space or something similar.
Two part question:
Is there a better solution than going through all of my code calling
Replace
on every string that is to be executed as a query?Why is my current implementation failing? - I still get syntax error in query expression even when escaping the single quote to a space.
"Insert into tblTest Values('tourette''s');"
is what @Tim is trying to indicate - note the 2 single quotes – Scute