Others

How to Fix Microsoft Access VBA Error When Exporting Attachments

WPS EditorWPS Editor Oct 12, 2026 870 views

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.

How to Fix Microsoft Access VBA Error When Exporting Attachments
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 you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 in Microsoft Access to open the Visual Basic for Applications (VBA) Editor.

2
Locate the SQL String

Find the line of code in your procedure (e.g., ExtractAllAttachments) where the SQL query is defined.

3
Add Brackets to References

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

4
Assign to a String Variable

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.

Correct Table and Field Name References in the SQL Statement
Debugging Tip: Hover over your strSQL variable while in debugging mode or print it to the Immediate Window using 'Debug.Print strSQL' to spot any missing spaces or typos before the query runs.
Free Microsoft Office alternative

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. 1. Download the Suite: Visit the official WPS Office website and download the free installation package.
  2. 2. Install WPS Office: Follow the on-screen prompts to complete the lightweight installation process.
  3. 3. Analyze Exported Data: Open WPS Spreadsheet to easily view, manage, and analyze any CSV or Excel files generated from your database exports.
Highly compatible with Microsoft Excel, Word, and PowerPoint file formats.Robust spreadsheet tools for managing and analyzing your exported database data.Lightweight installation that runs quickly and smoothly on most devices.Familiar and intuitive user interface for a seamless transition.
microsoft office alternative - wps office

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.