Fix Access VBA Error 2434: Export Filtered Query to Excel
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.

- 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.
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.
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.
Press Alt + F11 in Access to open the VBA Editor and locate the module containing your export loop.
Declare your variables using 'Dim db As DAO.Database' and 'Dim qdf As DAO.QueryDef'.
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 & "'").
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".
At the end of the loop iteration, ensure you clean up the database by running db.QueryDefs.Delete "TempExportQry".

Reference a Hidden Form Control for the Parameter
Pass the parameter value to a hidden text box on an open form, allowing the saved query to read the criteria directly from the user interface.
Export Data Using Excel Automation (CopyFromRecordset)
Bypass DoCmd entirely by opening an Excel instance through VBA and writing a filtered DAO Recordset directly into the worksheet.
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. Download and Install: Get WPS Office from the official website and run the quick installation.
- 2. Open Exported Files: Locate the .xlsx files generated by your Access VBA script and open them directly in WPS Spreadsheet.
- 3. Analyze Data: Use built-in data filters, formulas, and PivotTables in WPS to finalize your broker firm reports.

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).




