How to Update SQL Text Containing Quotation Marks in Access VBA
Question details
The user needs to successfully execute an Access SQL UPDATE statement where the text value contains quotation marks or apostrophes, and correctly format date literals.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Running a VBA script to update database records with text strings that include special characters like single quotes, alongside date criteria.
- Observed behavior
- The SQL UPDATE statement fails or throws a syntax error because the unescaped quotation marks prematurely terminate the text string, and dates may be misinterpreted based on regional settings.
Ensure you have backed up your Access database before testing bulk UPDATE queries, and verify the exact names of your target tables and fields.
Escape Apostrophes Using the Replace Function
The safest and most common method to handle single quotes (apostrophes) in SQL text is to double them up using VBA's Replace function.
In SQL, single quotes are used to enclose string literals. If the string itself contains a single quote, it tricks the SQL engine into thinking the string has ended. Doubling the quote tells the engine to treat it as a literal character.
Press Alt + F11 in Access to open the VBA editor and navigate to the module containing your SQL execution code.
Wrap your text variable in single quotes and use the Replace function to double any existing apostrophes. Use the syntax: " Set Field='" & Replace(sText, "'", "''") & "'"
Ensure your date variable is formatted properly so regional settings do not cause errors. Append the WHERE clause like this: " WHERE MDate=#" & Format(Me.txtDate, "yyyy-mm-dd") & "#"
Run the complete statement using DoCmd.RunSQL. Your final code should look like: DoCmd.RunSQL "UPDATE table1 SET Field='" & Replace(sText, "'", "''") & "' WHERE MDate=#" & Format(Me.txtDate, "yyyy-mm-dd") & "#"

Enclose Text in Double Quotation Marks Using Chr(34)
If your text contains single quotes but no double quotes, you can safely enclose the entire string in double quotation marks using the ASCII code Chr(34).
Looking for a Lightweight Alternative to Microsoft Office?
While WPS Office does not include a database management tool like Access, it offers a powerful, free, and lightweight suite for your everyday document, spreadsheet, and presentation needs. It's fully compatible with Microsoft Office formats, allowing for a seamless transition without the hefty subscription fees.

Frequently Asked Questions
Why does an apostrophe break my SQL statement in Access VBA?
In SQL, single quotes (apostrophes) are used to define the beginning and end of a text string. If your text contains an apostrophe, SQL interprets it as the end of the string, causing a syntax error for the remaining text.
How do I handle regional date format issues in Access VBA SQL?
Access SQL expects dates in the US format (mm/dd/yyyy) or the ISO standard (yyyy-mm-dd). Use the VBA Format function, like Format(MyDate, "yyyy-mm-dd"), before appending the date literal to your SQL string.
What are parameterized queries and should I use them?
Parameterized queries use placeholders (like parameters in a QueryDef) instead of directly concatenating user input into the SQL string. They are highly recommended for production environments because they automatically handle special characters like quotes and protect your database against SQL injection.




