Others

How to Run an Access Append Query in VBA Using Form Controls

Chanuka GeekiyanageChanuka Geekiyanage Oct 26, 2026 869 views

Question details

The user needs to execute an append query in MS Access using VBA and values from form controls, but encounters errors because the database engine does not automatically recognize form references.

How to Run an Access Append Query in VBA Using Form Controls
Product
Microsoft Access
Device & OS
not provided
Scenario
Attempting to insert new records into a target table via VBA by referencing the current values of text boxes and combo boxes on an active Access form.
Observed behavior
The append query fails to execute because DAO operates at the database engine level and cannot directly interpret UI form control variables inside a raw SQL query string.
Before you start

Ensure your Access database is stored in a Trusted Location so VBA code can run, and double-check the exact names of your target table and form controls before building the SQL statement.

Solution 1Recommended

Construct the SQL Statement by Concatenating Form Controls

Directly build the SQL string in VBA by appending the current values of the form controls into the query text before executing it.

DAO executes at the database engine level, meaning it cannot resolve expressions like 'Forms!FormName!ControlName' directly within an SQL string literal. To fix this, you must construct the string dynamically by concatenating the form values into the VBA string variable before passing it to the Execute method.

1
Declare variables

Open your VBA editor and declare a String variable for your SQL statement and a DAO.Database variable (e.g., Dim sql As String, dbs As DAO.Database).

2
Concatenate the SQL string

Build your INSERT INTO statement by stepping outside the string literal to append form references. Use single quotes for text values. For example: sql = "INSERT INTO [TargetTable] (CompanyName) VALUES ('" & Forms!Form1!Text1 & "')"

3
Set the database reference

Assign the current database to your DAO variable by typing: Set dbs = CurrentDb

4
Execute the query

Run the SQL command using the dbFailOnError option to ensure execution errors are caught: dbs.Execute sql, dbFailOnError

Construct the SQL Statement by Concatenating Form Controls
Prevent SQL Injection: While concatenation works for simple cases, using Parameterized Queries (QueryDefs) is much safer for production code. It prevents errors caused by stray apostrophes in user input and protects against SQL injection.
Free Microsoft Office alternative

Looking for a Reliable and Free Office Suite? Try WPS Office

While Microsoft Access handles complex relational databases, most everyday data management, tracking, and reporting tasks can be completed much faster using powerful spreadsheet software. WPS Office provides a lightweight, completely free, and highly compatible alternative to Microsoft Office.

  1. 1. Download and Install: Get WPS Office for free from the official website and install it on your device in minutes.
  2. 2. Open Existing Files: Easily open your exported database tables (.csv or .xlsx) directly in WPS Spreadsheet without format loss.
  3. 3. Manage Data Quickly: Use built-in data forms, pivot tables, and advanced formulas to manipulate and analyze your data seamlessly.
100% compatible with Microsoft Excel (.xlsx), Word, and PowerPoint formats.Lightweight software that runs smoothly on Windows, Mac, and Linux without consuming excessive system resources.Built-in advanced spreadsheet functions and data visualization tools for efficient data management.Completely free to use with a familiar, easy-to-navigate tabbed interface for seamless migration.
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a "Too few parameters" error when running an Access query via VBA?

This error happens because the DAO database engine does not recognize Access form controls embedded directly inside the SQL text. You must extract the control values and concatenate them into the SQL string or use a QueryDef object to evaluate them properly.

What is the purpose of dbFailOnError in the DAO Execute method?

Adding dbFailOnError ensures that if the action query fails (due to key violations, locked records, or type mismatches), VBA generates a run-time error that you can trap, and automatically rolls back any partial changes.

Do I need to format dates when concatenating them into an Access SQL string?

Yes. When concatenating dates into an Access SQL string in VBA, you must enclose the date values in hash symbols (#) and ideally format them in the US date format (mm/dd/yyyy) to prevent the database engine from misinterpreting the days and months.

Can I use parameterized queries instead of string concatenation in Access VBA?

Yes. Using a saved QueryDef and passing parameters dynamically is the recommended approach for production databases. It automatically resolves issues with apostrophes in text fields and secures your database against SQL injection attacks.