Create and Open a Windows Folder from an Access VBA Form
Question details
Create a Windows folder using values from an Access form and open it with a separate button.

- Product
- Microsoft Access
- Device & OS
- Windows
- Scenario
- A form bound to the demandes table has fields for poste, sr, and acheteur that should determine the folder name.
- Observed behavior
- The user wants one button to create the folder and another to open the matching folder in File Explorer.
Create a parent folder such as C:\Test and confirm that you have permission to create folders there. In Access, check that the form contains controls named poste, sr, and acheteur, or adjust the names in the code.
Add VBA buttons to create and open the folder
Use this approach when the folder name should come from the current Access form record.
The code replaces invalid Windows filename characters, handles empty values and reserved names, and checks whether the target already exists. Both buttons calculate the path from the values currently displayed on the form.
Open the form in Design View and add two command buttons. In each button's Property Sheet, set its Name to cmdCreateFolder or cmdOpenFolder. On the Event tab, set On Click to [Event Procedure] for each button.
In the Property Sheet, click the ellipsis beside either On Click event to open the form's VBA module. Paste the following code into that module, changing ROOT_FOLDER if needed. If Access already created empty procedures with the same button names, replace them rather than duplicating them: Option Compare Database Option Explicit Private Const ROOT_FOLDER As String = "C:\Test" Private Function CurrentFolderPath() As String Dim folderName As String Dim invalidChars As String Dim baseName As String Dim i As Long folderName = Trim$(Nz(Me!poste.Value, "") & " " & Nz(Me!sr.Value, "") & " " & Nz(Me!acheteur.Value, "")) If Len(folderName) = 0 Then Err.Raise vbObjectError + 1000, , "Enter at least one folder field value." End If invalidChars = "\/:*?""<>|" For i = 1 To Len(invalidChars) folderName = Replace$(folderName, Mid$(invalidChars, i, 1), "_") Next i For i = 0 To 31 folderName = Replace$(folderName, Chr$(i), "_") Next i Do While Len(folderName) > 0 If Right$(folderName, 1) <> "." And Right$(folderName, 1) <> " " Then Exit Do folderName = Left$(folderName, Len(folderName) - 1) Loop If Len(folderName) = 0 Then Err.Raise vbObjectError + 1001, , "The field values do not produce a valid folder name." End If baseName = folderName If InStr(baseName, ".") > 0 Then baseName = Left$(baseName, InStr(baseName, ".") - 1) If InStr(1, "|CON|PRN|AUX|NUL|COM1|COM2|COM3|COM4|COM5|COM6|COM7|COM8|COM9|LPT1|LPT2|LPT3|LPT4|LPT5|LPT6|LPT7|LPT8|LPT9|", "|" & baseName & "|", vbTextCompare) > 0 Then folderName = "_" & folderName End If CurrentFolderPath = ROOT_FOLDER & "\" & folderName End Function Private Sub cmdCreateFolder_Click() On Error GoTo HandleError Dim folderPath As String folderPath = CurrentFolderPath() If (GetAttr(ROOT_FOLDER) And vbDirectory) = 0 Then Err.Raise vbObjectError + 1002, , "The parent path is not a folder." End If If Len(Dir$(folderPath, vbDirectory)) = 0 Then MkDir folderPath MsgBox "Folder created: " & folderPath, vbInformation ElseIf (GetAttr(folderPath) And vbDirectory) <> 0 Then MsgBox "The folder already exists: " & folderPath, vbInformation Else MsgBox "A file already exists at this path: " & folderPath, vbExclamation End If Exit Sub HandleError: MsgBox "Could not create the folder: " & Err.Description, vbExclamation End Sub Private Sub cmdOpenFolder_Click() On Error GoTo HandleError Dim folderPath As String folderPath = CurrentFolderPath() If Len(Dir$(folderPath, vbDirectory)) = 0 Then MsgBox "Create the folder first: " & folderPath, vbInformation ElseIf (GetAttr(folderPath) And vbDirectory) = 0 Then MsgBox "The path points to a file, not a folder: " & folderPath, vbExclamation Else Shell "explorer.exe """ & folderPath & """", vbNormalFocus End If Exit Sub HandleError: MsgBox "Could not open the folder: " & Err.Description, vbExclamation End Sub
In the VBA editor, select Debug > Compile, then save the form. Switch to Form View, select a record, click the create button, and then click the open button.

Use WPS Office for documents and spreadsheets
WPS Office is a free, lightweight alternative for everyday document and spreadsheet work, with a familiar interface and support for common Microsoft Office formats to ease migration. It does not run Access form VBA or replace the Access database solution above.

Frequently Asked Questions
Where should I paste the button code in Access?
Open the form in Design View, select a button, and click the ellipsis next to On Click on the Property Sheet's Event tab. Paste the procedures and helper function into the form's VBA module.
What happens if the folder already exists?
The create button checks the target path before calling MkDir. If the folder exists, it displays a message instead of trying to create it again.
Why does Access report a compile error?
In the VBA editor, choose Debug > Compile and inspect the highlighted line. Check for duplicate Option statements or button procedures, and confirm that the button names match cmdCreateFolder and cmdOpenFolder.
Can I change the parent folder?
Yes. Change the ROOT_FOLDER constant in the form module to an existing folder path without a trailing backslash, and confirm that you have permission to create folders inside it.




