logo
search
VBA & Macro Problems

Fix Access VBA Error 2434: Export Filtered Query to Excel

Phi Hung VoPhi Hung Vo Oct 9, 2026 869 views

Question details

The user needs to export a filtered database query to separate Excel files for different broker firms but encounters runtime error 2434 when using the DoCmd.SetParameter method.

How to Fix Access VBA Runtime Error 2434 When Exporting Filtered Queries to Excel
Product
Microsoft Access
Device & OS
not provided
Scenario
Automating the export of a database query to distinct Excel spreadsheets based on a loop of parameter values (e.g., broker firms).
Observed behavior
Using DoCmd.SetParameter causes runtime error 2434. Omitting the parameter results in the system prompting the user manually or exporting empty files.
Before you start

Ensure your database is backed up before running VBA loops, and verify that the Microsoft DAO Object Library is enabled in your VBA editor's References.

Solution 1Recommended

Use a DAO QueryDef to Dynamically Filter and Export Data

Avoid DoCmd.SetParameter by creating a temporary query definition in VBA that dynamically applies the SQL filter before executing the export command.

The DoCmd.TransferSpreadsheet method often struggles to resolve temporary VBA parameters set by DoCmd.SetParameter, leading to error 2434. A more robust approach is to manipulate a DAO QueryDef object to hold your SQL string with the specific broker firm hardcoded into the WHERE clause for that iteration of the loop.

1
Open the VBA Editor

Press Alt + F11 in Access to open the VBA Editor and locate the module containing your export loop.

2
Define DAO Variables

Declare your variables using 'Dim db As DAO.Database' and 'Dim qdf As DAO.QueryDef'.

3
Construct the Dynamic SQL

Inside your loop, construct a SQL string that injects the current broker firm value directly into the WHERE clause (e.g., strSQL = "SELECT * FROM Table WHERE Broker = '" & currentBroker & "'").

4
Create and Export the Temporary Query

Assign the SQL string to a temporary QueryDef (e.g., db.CreateQueryDef("TempExportQry", strSQL)), then use DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "TempExportQry", "C:\Path\" & currentBroker & ".xlsx".

5
Delete the Temporary Query

At the end of the loop iteration, ensure you clean up the database by running db.QueryDefs.Delete "TempExportQry".

Use a DAO QueryDef to Dynamically Filter and Export Data
Best Practice: Using a temporary QueryDef ensures the Access database engine can fully parse the query requirements independent of the VBA runtime state.
Free Microsoft Office alternative

Try WPS Office for Your Spreadsheet and Data Needs

While Microsoft Access is used for building relational databases, exporting those records often requires a powerful spreadsheet tool to analyze and format the data. WPS Office provides a free, lightweight, and highly compatible alternative to Microsoft Office for opening, editing, and managing your exported Excel files.

  1. 1. Download and Install: Get WPS Office from the official website and run the quick installation.
  2. 2. Open Exported Files: Locate the .xlsx files generated by your Access VBA script and open them directly in WPS Spreadsheet.
  3. 3. Analyze Data: Use built-in data filters, formulas, and PivotTables in WPS to finalize your broker firm reports.
Fully compatible with Microsoft Excel formats (.xlsx, .xls, .csv)Lightweight installation with a familiar, easy-to-navigate tabbed interfaceAdvanced data analysis tools, pivot tables, and charting capabilitiesFree to use, making it the perfect companion for your exported database reports
microsoft office alternative - wps office

Frequently Asked Questions

Why does DoCmd.SetParameter trigger runtime error 2434?

Error 2434 occurs because methods like TransferSpreadsheet execute outside the immediate VBA scope where the parameter was defined. The database engine cannot resolve the parameter context when it attempts to run the export command.

Can I export multiple queries to the same Excel workbook?

Yes. If you specify the same file path in multiple DoCmd.TransferSpreadsheet commands, Access will add each query as a new worksheet within the target Excel workbook, using the query's name as the sheet name.

How do I fix the 'User-defined type not defined' error when using DAO?

This error means the DAO library is missing from your VBA project. Open the VBA Editor, click Tools > References, and check the box for 'Microsoft Office Access database engine Object Library' (or 'Microsoft DAO 3.6 Object Library' in older versions).