Apostrophes in code

G

Guest

I have a form called frmBudget. This form has an unbound text box called
memBudgetNote for the user to enter notes, etc. When the user is finished I
store the entered note in a table called tblHistory. I use the following
code to store the note:

DoCmd.RunSQL "UPDATE tblHistory " & _
"Set BudgetNote = '" & Forms!frmBudget!memBudgetNote & " ';"

It works fine until the user enters an apostrophe, e.g. "It's in the file"
or "Bob's car". This produces the following error:
Run-time error '3075': Syntax error (missing operator) in query expression
"Bob's car ';'.

How can I fix this problem?
 
M

Marshall Barton

Del said:
I have a form called frmBudget. This form has an unbound text box called
memBudgetNote for the user to enter notes, etc. When the user is finished I
store the entered note in a table called tblHistory. I use the following
code to store the note:

DoCmd.RunSQL "UPDATE tblHistory " & _
"Set BudgetNote = '" & Forms!frmBudget!memBudgetNote & " ';"

It works fine until the user enters an apostrophe, e.g. "It's in the file"
or "Bob's car". This produces the following error:
Run-time error '3075': Syntax error (missing operator) in query expression
"Bob's car ';'.


You have to double up any apostrophes in the string:

DoCmd.RunSQL "UPDATE tblHistory Set BudgetNote='" & _
Replace(Forms!frmBudget!memBudgetNote,"'","''") & " '"
 
G

Guest

Thank you, I'll try that.
--
Thank you,
Del


Marshall Barton said:
You have to double up any apostrophes in the string:

DoCmd.RunSQL "UPDATE tblHistory Set BudgetNote='" & _
Replace(Forms!frmBudget!memBudgetNote,"'","''") & " '"
 

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