VBA & Macro Problems

How to Create Nested Folders Automatically Using Access VBA

Steve KSteve K Nov 2, 2026 871 views

Question details

The user needs a method to create an entire directory structure from a full path string using VBA, avoiding errors when parent folders are missing.

How to Create Nested Folders Automatically Using Access VBA
Product
Microsoft Access
Device & OS
not provided
Scenario
Developing a VBA script or macro to generate multiple nested folders at once based on a provided complete path, such as C:\Folder1\Folder2.
Observed behavior
The standard VBA MkDir statement only creates one directory level at a time and returns run-time error 76 if the parent folder does not exist.
Before you start

Ensure you have basic familiarity with the VBA Editor in Microsoft Access and have backed up your database before implementing and running custom code modules.

Solution 1Recommended

Create a Custom VBA Function to Build Folders Sequentially

Use a custom routine that parses the folder path and creates missing directories level by level from the root downward.

Because the native MkDir function fails if the parent directory does not exist, creating a sequential loop is necessary. This approach splits the path string by the backslash character and checks each directory level.

1
Open the VBA Editor

Open your Access database and press ALT + F11 to launch the Visual Basic for Applications (VBA) Editor.

2
Insert a New Module

Click 'Insert' from the top menu bar and select 'Module' to create a blank canvas for your code.

3
Write the Path Splitting Logic

Write a custom Sub or Function that accepts a string parameter for the full path. Use the built-in VBA `Split` function to separate the path using `\` as the delimiter.

4
Iterate and Check Directory Existence

Create a `For` loop to iterate through the resulting array. Reconstruct the path step-by-step and use `Dir(currentPath, vbDirectory)` to check if each level already exists.

5
Create Missing Folders

If `Dir` returns an empty string (meaning the folder does not exist), execute the `MkDir` statement for that specific level before allowing the loop to move to the next iteration.

Create a Custom VBA Function to Build Folders Sequentially
Eliminate Run-Time Errors: By verifying and creating each folder level sequentially, this method effectively prevents the run-time error 76 commonly associated with the standalone MkDir statement.
Free Microsoft Office alternative

Switch to WPS Office for a Lightweight and Powerful Productivity Suite

While Microsoft Access handles complex database tasks, for comprehensive document, spreadsheet, and presentation management, WPS Office provides a free, highly compatible alternative. WPS Spreadsheet fully supports VBA macros, making it easy to migrate your file management automation scripts seamlessly.

  1. 1. Visit the Official Site: Go to the official WPS Office website.
  2. 2. Download the Installer: Click the free download button suitable for your operating system.
  3. 3. Install and Automate: Run the installer, open WPS Spreadsheet, and press ALT + F11 to start using VBA macros instantly.
Fully compatible with Microsoft Excel, Word, and PowerPoint formatsBuilt-in robust support for VBA and macros to automate daily file tasksLightweight application that installs quickly and runs smoothly on all PCsFamiliar user interface requiring zero learning curve for Office users
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get run-time error 76 with MkDir?

Error 76, which stands for 'Path not found', occurs because the native `MkDir` function attempts to create a new folder inside a parent directory that does not currently exist on your hard drive. It cannot build the intermediate folders automatically.

Can I create multiple nested directories at once without using VBA?

Yes, on Windows systems you can use the Command Prompt. Typing the `mkdir` or `md` command followed by your full path (e.g., `md C:\Folder1\Folder2\Folder3`) will automatically generate any missing parent directories in one go.

Does WPS Office support VBA for similar automation tasks?

Yes, WPS Spreadsheet provides comprehensive support for VBA (Visual Basic for Applications). You can write, record, and execute macros to automate file system tasks, including creating sequential folders via `MkDir`, using the exact same logic.

Do I need to enable specific references to use the Split function in VBA?

No, the `Split` function is a built-in member of the standard VBA library. You can use it immediately in your code without having to enable any additional references in the 'Tools' menu.