First check these two lines.
"VALUES ( " & "'" & me.testparam & "'" & ");" "VALUES ( '" & me.testparam & "');"
Both will create the same line. The difference for me is that my brain understands the second version faster.
Now, here's what the comments tell you to do ... replace each single quote in the original string with two single quotes. I added Debug.Print
so that you can view the finished line in the Immediate window (go there with Ctrl + g) ... you can see the actual line, rather than trying to imagine what it looks like.
qr = "INSERT INTO tblExample VALUES ( '" & _ Replace(Me.testparam, "'", "''" & "');" Debug.Print qr db.Execute qr, dbFailOnError
Since I assumed that db
is a variable of the DAO.Database
object, I turned on the dbFailOnError
option. You must include an error handler in your code to resolve any dbFailOnError
problems.
When you encounter a problem with the VBA function in the request, go to the Immediate window and check the expression of the function there. This causes a compilation error "Expected: list separator or)":
? Replace("Tourette's", "'", " "'" ")
But it works:
? Replace("Tourette's", "'", "''") Tourette''s
I mentioned this because it is useful in general, and also because your title starts with "Reset unwanted characters, mostly single quotes." Therefore, if you want to remove / replace other characters, not single quotes, experiment in the Immediate window until you find the Replace()
expression that works. Then use this expression in the query.
For example, if unwanted characters include line breaks ...
MyString = "foo" & vbCrlf & "bar" : ? MyString foo bar ? Replace(MyString, Chr(13) & Chr(10), " ") foo bar
Note. I used Chr(13) & Chr(10)
instead of vbCrlf
as the search target, because the db mechanism can use the Chr()
function, but does not know about the named constant ( vbCrlf
).