How to Insert or Update Microsoft Access Table Values with VBA
Question details
The user needs to formulate correct SQL statements in Access VBA to distinguish between inserting a new record and updating an existing record without encountering syntax errors.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Executing a VBA procedure to dynamically add or modify database records using SQL strings.
- Observed behavior
- The VBA procedure fails because the SQL statement incorrectly uses a WHERE clause for an INSERT operation, or fails to format numeric and text values properly.
Always back up your Access database before running new VBA scripts to prevent accidental data corruption or loss.
Use Correct INSERT Statement Syntax for New Records
Apply the proper SQL syntax for appending new records by ensuring no WHERE clause is used and data types are formatted correctly.
When inserting a new record into an Access table, you are creating a completely new row. Therefore, using a WHERE clause is logically invalid. You must specify the target fields and pass the corresponding values, taking care to format numeric and text variables appropriately in your VBA string.
Determine the fields you are populating (e.g., questionsNum and SelectedAnswer) and verify whether they are numeric or text in the table design.
Construct the VBA string. For numeric values, do not use quotes. For text values, enclose them in single quotes. Example: strSQLINSERT = "INSERT INTO tbl_questions_answers (questionsNum, SelectedAnswer) VALUES (" & Me!questionsNum & ", '" & strChosenAnswer & "')"
Run the SQL command using DoCmd.RunSQL or CurrentDb.Execute in your VBA module to append the new record.

Use Parameterized Queries for Safety
Implement parameterized queries to automatically handle problematic characters like apostrophes and protect against SQL injection.
Manage Data Easily with WPS Office
While WPS Office does not include a database management tool like Microsoft Access, it is a highly compatible, free, and lightweight alternative for Word, Excel, and PowerPoint. If you need to manage tabular data without writing complex VBA or SQL, WPS Spreadsheet offers powerful functions, pivot tables, and intuitive data validation.
- 1. Download WPS Office: Visit the official WPS website to download and install the free Office suite.
- 2. Open WPS Spreadsheet: Launch WPS Spreadsheet to import or create your data tables.
- 3. Organize Your Data: Use built-in data sorting, filtering, and pivot tables to manage large datasets effortlessly without VBA code.

Frequently Asked Questions
Why do I get a syntax error when using an apostrophe in my VBA SQL insert?
If your text data contains an apostrophe, it prematurely terminates the SQL string wrapper. You can fix this by replacing single quotes with double single quotes using the Replace function in VBA, or by utilizing parameterized queries.
Can I use a WHERE clause in an INSERT INTO statement?
No. A WHERE clause is used to identify existing records, which applies exclusively to UPDATE, DELETE, or SELECT statements. Because INSERT INTO creates a completely new record, adding a WHERE clause will result in an error.
How do I update an existing record instead of inserting a new one?
To update an existing record, use the UPDATE statement combined with a SET clause to assign the new values, and a WHERE clause to specify exactly which record (usually identified by the Primary Key) should be modified.




