Form allows text entry; how to stop apostrophes from breaking SQL?

R

Rachel Garrett

I have a form with a text box (Memo data type) that allows user
comments. I also run some SQL later on that inserts these comments
elsewhere. If the user types apostrophes (like in the words don't,
let's, they're, etc.), then this will break the SQL. I would like to
write some VBA code to put escape characters into the user's comments
*before* it gets sent to SQL. Any suggestions?
 
J

John W. Vinson

I have a form with a text box (Memo data type) that allows user
comments. I also run some SQL later on that inserts these comments
elsewhere. If the user types apostrophes (like in the words don't,
let's, they're, etc.), then this will break the SQL. I would like to
write some VBA code to put escape characters into the user's comments
*before* it gets sent to SQL. Any suggestions?

Well, just don't let it break the SQL. Use " to delimit the inserted string
rather than '.

Or, use

Replace(memotext, "'", "''")

to replace all instances of ' with two consecutive ', which will be parsed
back to a single apostrophe.

Perhaps you could post your current code or the SQL of the query.
 
Joined
Sep 3, 2009
Messages
4
Reaction score
0
Doesn't that cause it to still display as double single quotes? or is that an escape character? I mean, I want my users to be able to type in the txtbx just as if they were typing in word... no restrictions, no dangers of SQL stupidity. Why doesn't SQL simply treat the data as objects? I mean, I can't image why they'd design it so that entering values in a text box has any potential to run a query...shouldn't they just have another textboxish type object call SQLbox or something. Access 2014 maybe?
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top