How to Fix Microsoft Access VBA Error When Exporting Attachments
Question details
The user needs to resolve an 'External name not defined' error that occurs when running a VBA script to export attachments from a database table to a local folder.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Using Access VBA code to automate the extraction and export of file attachments from a table field into a designated folder.
- Observed behavior
- The VBA code fails to execute and displays an 'External name not defined' error message due to incorrect references in the table, field, or SQL statement.
Before modifying your VBA code, verify that the destination folder on your local drive (such as C:\Attachments) already exists and that your user account has write permissions for that directory.
Correct Table and Field Name References in the SQL Statement
Fix syntax errors in your VBA code by properly quoting and bracketing the table and attachment field names.
The 'External name not defined' error most commonly triggers when VBA cannot parse spaces or special characters in table or field names within the SQL string.
Press ALT + F11 in Microsoft Access to open the Visual Basic for Applications (VBA) Editor.
Find the line of code in your procedure (e.g., ExtractAllAttachments) where the SQL query is defined.
Enclose both the table name and the attachment field name in square brackets to ensure they are read correctly (for example, SELECT * FROM [My Table Name]).
Assign the SQL statement to a dedicated string variable (like strSQL) instead of writing it inline. This allows you to inspect the exact string being passed during execution.

Execute the Procedure via the Immediate Window
Run the extraction procedure directly from the Immediate Window to ensure the correct parameters are being supplied.
Try WPS Office for Your Document and Data Needs
While WPS Office does not include a direct database program like Microsoft Access, it offers a powerful, free, and lightweight suite for all your document, spreadsheet, and presentation tasks with excellent Microsoft Office compatibility.
- 1. Download the Suite: Visit the official WPS Office website and download the free installation package.
- 2. Install WPS Office: Follow the on-screen prompts to complete the lightweight installation process.
- 3. Analyze Exported Data: Open WPS Spreadsheet to easily view, manage, and analyze any CSV or Excel files generated from your database exports.

Frequently Asked Questions
Why does VBA say 'External name not defined'?
This error typically occurs when the VBA compiler cannot find a referenced table, field, or variable. In SQL statements, omitting square brackets around names that contain spaces is a common trigger for this error.
How do I view the Immediate Window in Access VBA?
To open the Immediate Window, first open the VBA Editor by pressing ALT + F11, then press CTRL + G or select View > Immediate Window from the top navigation menu.
Can I export Access attachments to a network drive?
Yes, you can export attachments to a network folder by replacing the local drive path (e.g., C:\Attachments) with a mapped network drive or UNC path (e.g., \\Server\Folder) in your VBA procedure call, provided you have the necessary write permissions.




