Others

How to Insert or Update Microsoft Access Table Values with VBA

John WilsonJohn Wilson Oct 23, 2026 870 views

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.

How to Insert or Update Microsoft Access Table Values with VBA
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.
Before you start

Always back up your Access database before running new VBA scripts to prevent accidental data corruption or loss.

Solution 1Recommended

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.

1
Identify fields and data types

Determine the fields you are populating (e.g., questionsNum and SelectedAnswer) and verify whether they are numeric or text in the table design.

2
Format the SQL string

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

3
Execute the statement

Run the SQL command using DoCmd.RunSQL or CurrentDb.Execute in your VBA module to append the new record.

Use Correct INSERT Statement Syntax for New Records
Data Type Formatting: If your primary key (e.g., questionsNum) is a text field instead of a numeric field, you must also enclose the VBA variable in single quotes within the SQL string.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website to download and install the free Office suite.
  2. 2. Open WPS Spreadsheet: Launch WPS Spreadsheet to import or create your data tables.
  3. 3. Organize Your Data: Use built-in data sorting, filtering, and pivot tables to manage large datasets effortlessly without VBA code.
Highly compatible with Microsoft Office formats (XLSX, DOCX, PPTX)Manage tabular data easily with advanced Spreadsheet functions and filtersLightweight installation and incredibly fast startup speedFamiliar user interface for a seamless transition
microsoft office alternative - wps office

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.