Apostrophes in code

  • Thread starter Thread starter Guest
  • Start date Start date
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?
 
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,"'","''") & " '"
 
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,"'","''") & " '"
 
Back
Top