Others

How to Update SQL Text Containing Quotation Marks in Access VBA

Amos GikundaAmos Gikunda Nov 4, 2026 869 views

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.

How to Update SQL Text Containing Quotation Marks in Access VBA
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.
Before you start

Ensure you have backed up your Access database before testing bulk UPDATE queries, and verify the exact names of your target tables and fields.

Solution 1Recommended

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.

1
Open your VBA editor

Press Alt + F11 in Access to open the VBA editor and navigate to the module containing your SQL execution code.

2
Modify the SQL string

Wrap your text variable in single quotes and use the Replace function to double any existing apostrophes. Use the syntax: " Set Field='" & Replace(sText, "'", "''") & "'"

3
Format the date criteria

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") & "#"

4
Execute the query

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") & "#"

Escape Apostrophes Using the Replace Function
Date Formatting Best Practice: Always format date literals in VBA SQL as yyyy-mm-dd to avoid regional setting misinterpretations. Access SQL inherently expects US format (mm/dd/yyyy) or ISO standard.
Free Microsoft Office alternative

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.

Fully compatible with Microsoft Excel, Word, and PowerPoint formats (.xlsx, .docx, .pptx).Lightweight installation that runs smoothly on Windows, Mac, Linux, iOS, and Android.Familiar and intuitive user interface with zero learning curve.Advanced data analysis and macro support in WPS Spreadsheet for handling large datasets.
microsoft office alternative - wps office

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.