Solved

Access: how to escape single quotes

Posted on 2009-06-29
6
233 Views
Last Modified: 2013-11-27
Hi x-perts

Here is a simple SQL string:

It obviously returns an error, when a text string contains a quotation mark itself (like dog's)

How can I fox this?

Thanks
SQL = "INSERT INTO Betas (" & strFields & ") VALUES (" & strBetas & ")"
 
strFields = "[field1],[field2]"
 
strBetas = firstNumber & "'" & textValue & "'"
 
textValue = "dog's"

Open in new window

0
Comment
Question by:andy7789
  • 3
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 150 total points
ID: 24742089
replace the single quote with 2 singlequotes
textValue = "dog''s"  
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24742107
Aneesh is correct, but you just in case you didn't already notice, you will need to have a comma in strBetas.
SQL = "INSERT INTO Betas (" & strFields & ") VALUES (" & strBetas & ")"
 
strFields = "[field1],[field2]"
 
strBetas = firstNumber & ",'" & textValue & "'"
 
textValue = "dog''s"

Open in new window

0
 

Author Comment

by:andy7789
ID: 24742160
thank you, but it doesn't work

1) coma is just a typo here (not in my actual code)
2) i cannot modify the dog's string, because it is taken as a field value from another table

is there a function to escape quotes like in php?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 59

Accepted Solution

by:
Kevin Cross earned 350 total points
ID: 24742183

strBetas = firstNumber & ",'" & replace(textValue, "'", "''") & "'"

Open in new window

0
 

Author Comment

by:andy7789
ID: 24742283
works great - thank you!
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24742288
You are welcome!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question