How to Create Nested Folders Automatically Using Access VBA
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.

- 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.
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.
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.
Open your Access database and press ALT + F11 to launch the Visual Basic for Applications (VBA) Editor.
Click 'Insert' from the top menu bar and select 'Module' to create a blank canvas for your code.
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.
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.
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.

Use the FileSystemObject for Folder Creation
Leverage the Microsoft Scripting Runtime for a robust, object-oriented approach to file system management.
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. Visit the Official Site: Go to the official WPS Office website.
- 2. Download the Installer: Click the free download button suitable for your operating system.
- 3. Install and Automate: Run the installer, open WPS Spreadsheet, and press ALT + F11 to start using VBA macros instantly.

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.




