How to Handle Quotation Marks in Access VBA INSERT Queries
Question details
The user needs to insert text containing single and double quotation marks into an Access database using VBA without breaking the SQL INSERT statement.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Inserting user-entered text containing apostrophes or double quotes into a database table via a dynamically built VBA SQL INSERT query.
- Observed behavior
- Concatenating user input with single quotes directly into an SQL string causes syntax errors, while manually replacing double quotes causes them to incorrectly appear duplicated in the saved table data.
Determine whether your SQL text values are delimited by single or double quotes, as this determines which specific character needs to be escaped in your VBA code.
Use Parameterized DAO QueryDefs
Using parameterized queries is the most secure and reliable method, as it automatically handles all quotation marks and protects against SQL injection.
Instead of concatenating user input directly into an SQL string, parameterization separates the executable code from the data. This guarantees that special characters, including both single and double quotes, are processed as literal text.
Open your VBA editor and declare your DAO Database and QueryDef objects: `Dim db As DAO.Database, qdf As DAO.QueryDef`.
Create an SQL string defining the parameters first. For example: `PARAMETERS [pNote] Text; INSERT INTO YourTable (NoteField) VALUES ([pNote]);`.
Assign the SQL string to your QueryDef object by using `Set qdf = db.CreateQueryDef("", strSQL)`.
Set the parameter value directly from your form control without any manual string replacement: `qdf.Parameters("[pNote]").Value = Me!txtNote`.
Run the query safely using `qdf.Execute dbFailOnError` to ensure the data is saved exactly as entered.

Escape Single Quotes Using the Replace Function
If you must build SQL strings dynamically, use the VBA Replace function to double up single quotes (apostrophes) so the database engine treats them as literal characters.
Looking for a Lightweight Alternative to Microsoft Office?
While Microsoft Access is a specialized database tool, many data management, automation, and macro tasks can be efficiently handled in a spreadsheet environment. WPS Office provides a highly compatible, fast, and free alternative for your daily document and data processing needs.
- 1. Download WPS Office: Visit the official WPS website and download the free installer for your operating system.
- 2. Install the Software: Run the downloaded file and follow the standard installation prompts to set up the suite.
- 3. Manage Data in Spreadsheets: Open WPS Spreadsheets to organize tabular data, perform data validation, and utilize macro-like features without the overhead of a full database management system.

Frequently Asked Questions
Why do apostrophes break my Access VBA SQL string?
When your SQL syntax wraps text values in single quotes, an apostrophe (single quote) in the user input acts as a string terminator. The database engine misinterprets the rest of the text as an invalid SQL command, resulting in a syntax error.
Should I also double up double quotation marks in my input?
Generally, no. If your SQL text values are enclosed in single quotes, double quotation marks inside that text are treated as literal characters and do not need to be doubled. Doing so will cause them to incorrectly appear twice in your saved data.
What is the best way to handle user inputs with special characters in VBA?
The most robust and secure approach is using parameterized queries (DAO QueryDefs). This method safely separates the SQL command from the user data, automatically handling any quotes or special characters without the need for manual string replacement.




