How to Fix Type Mismatch Errors in Microsoft Access VBA
Question details
The user needs to resolve a Run-time error 13 (Type Mismatch) that occurs in their VBA code while exporting Microsoft Access queries to Excel.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Running a VBA macro in Microsoft Access to export specific database queries to a Microsoft Excel file.
- Observed behavior
- The VBA script halts execution and displays a 'Type Mismatch' error, preventing the query data from successfully exporting to Excel.
Before modifying your VBA script, check your project references to confirm that the Microsoft Office Access database engine Object Library is enabled.
Explicitly Declare QueryDef as DAO.QueryDef
Disambiguate the QueryDef object by explicitly linking it to the Data Access Objects (DAO) library to prevent type mismatch conflicts with ADO.
In Microsoft Access VBA, if multiple object libraries (like DAO and ADO) are referenced, calling 'QueryDef' without specifying the library can cause Access to assume the wrong object type, resulting in a Type Mismatch error.
Press 'Alt + F11' while in Microsoft Access to open the Visual Basic for Applications (VBA) editor.
Find the section of your macro code where you declared the query definition variable, which likely looks like 'Dim RawTOL As QueryDef'.
Change the code to explicitly reference DAO by rewriting it as 'Dim RawTOL As DAO.QueryDef'.
Click 'Tools' > 'References' in the top menu and ensure that 'Microsoft Office Access database engine Object Library' (or 'Microsoft DAO Object Library') is checked.
Click 'Debug' > 'Compile [Your Project Name]' to apply the changes, then save the database and run the export macro again.

Verify and Match Variable Data Types
Ensure that the data types of the variables handling parameters in your VBA script perfectly match the expected data types in the Access query.
Discover WPS Office for Seamless Macro and Spreadsheet Management
While Microsoft Access is used for heavy relational database workloads, managing exported data, formatting reports, and automating tasks is often better suited for a spreadsheet application. WPS Office provides a powerful, free alternative to Microsoft Office, featuring robust VBA support in WPS Spreadsheet to handle all your macro-driven data workflows effortlessly.
- 1. Install WPS Office: Visit the official WPS website and download the free WPS Office suite to your computer.
- 2. Open Exported Data: Launch WPS Spreadsheet and seamlessly open your exported .xlsx or .csv database files without formatting loss.
- 3. Utilize Macro Tools: Navigate to the Developer tab to access the native VBA editor and streamline your data processing tasks.

Frequently Asked Questions
What does a 'Type Mismatch' error mean in VBA?
A Type Mismatch (Run-time error 13) happens when VBA attempts to assign a value to a variable that conflicts with its declared data type, or when an ambiguous object reference defaults to the incorrect software library.
How do I add the DAO object library to my Access database?
In the VBA editor, click 'Tools' > 'References' in the top menu bar. Scroll down and check the box for 'Microsoft Office [Version Number] Access database engine Object Library' (or 'Microsoft DAO 3.6 Object Library' in older Access versions), then click OK.
Why is DAO preferred over ADO for QueryDef in Access?
DAO (Data Access Objects) is the native library built specifically for the Microsoft Access database engine. It provides direct, optimized access to Access-specific objects like QueryDef, whereas ADO (ActiveX Data Objects) is a generalized library designed for communicating with a wide variety of OLE DB data sources.




