Introduction.
We have already seen how to create folders with the MkDir() DOS command and how to change the default folder to the active database’s folder using the ChDir() command.
Conversely, if we can create folders, we should also be able to remove them. The RmDir() command serves this purpose, though it is not as commonly used as MkDir(). Typically, folders or sub-folders are created to organize files for easier access when needed. Folder removal becomes necessary only when files are relocated or deleted, and freeing up disk space is required.
To understand its usage, you can try running the RmDir() command directly from the Immediate Window (Debug Window), as shown below:
RmDir "C:\MyFolder"
Validation Checks.
The RmDir() command has a built-in safety check. When executed, it first verifies whether the specified folder is completely empty—containing no files or subfolders. If the command runs successfully, it completes silently without displaying any message. However, if the folder is not empty or cannot be removed, one of the following two error messages will appear:
If the specified folder path does not exist, it will show a 'Path Not Found' Error Message.
- If the folder is not empty, then the message ‘Path/File access error’ is displayed.
Through Windows Explorer, we can remove a folder with all its sub-folders and files in one clean sweep. If this is done by mistake, then we can always restore them from the Recycle bin, also before emptying it.
Important Note: Folders or files deleted from a network location using Windows Explorer are not sent to the Recycle Bin. If you have the necessary access rights to delete items on the network, recovery cannot be done locally. In such cases, you must contact your Network Administrator to restore them from the most recent LAN backup. As a safeguard, many organizations allow users to create folders but restrict deletion rights to administrators, reducing the risk of accidental data loss.
A Custom Function.
Let’s create a small function named FolderDeletion() that will execute the RmDir()
DOS command with proper validation checks. You can add this function to your common utilities library for reuse.
-
Before physically removing a folder, the function will perform the following validations:
-
Check if the folder exists – Ensure the specified path is valid.
-
Confirm the folder is empty – The folder should not contain any subfolders or files.
-
Validate permissions – Confirm the user has rights to delete the folder.
-
Error handling – Provide meaningful messages if deletion fails.
-
The Function VBA Code:
Public Function DeleteFolder(ByVal strFolder As String) On Error Resume Next If Len(Dir(strFolder, vbDirectory)) > 0 Then 'Folder exists, ask for permission to delete the folder If (MsgBox("Deleting Folder: '" & strFolder & "', Proceed...?", vbOKCancel + vbDefaultButton2 + vbQuestion, "DeleteFolder()") = vbNo) Then 'User says not to delete the folder, exit program GoTo DeleteFolder_Exit Else 'Delete Folder RmDir strFolder If Err = 75 Then 'folder is not empty, have sub-folders or files MsgBox "Folder: '" & strFolder & "' is not empty, cannot be removed." GoTo DeleteFolder_Exit Else MsgBox "Folder: '" & strFolder & "' deleted." GoTo DeleteFolder_Exit End If End If Else MsgBox "Folder: '" & strFolder & "' Not found." End If DeleteFolder_Exit: On Error GoTo 0 End Function
If you want an alternative to the same result, we can use VBScript in Microsoft Access to do that. VB Script is mostly used in Web Pages for server-side actions. VB Script uses the FileSystemObject to manage Drives, Folders, & Files. We have used it for creating Text, Word, and Excel Files before.
You can find those examples in the following links:
VBScript Function: FolderCreation()
First, let us write a VBScript Function to create a Folder - C:\MyProjects.
Public Function FolderCreation(ByVal strFolder As String) Dim FSysObj, fldr On Error Resume Next 'arrange to capture the error so that it can be check 'Create the File System Object Set FSysObj = CreateObject("Scripting.FileSystemObject") 'Call the Create Folder Method of the File System Object with Folder Path as parameter Set fldr = FSysObj.CreateFolder(strFolder) 'if this action ended up with error code 58 then the folder already exists If Err = 58 Then MsgBox "Folder: '" & strFolder & "' already exists." GoTo FolderCreation_Exit Else MsgBox "Folder: " & strFolder & " created successfully." End If FolderCreation_Exit: On Error GoTo 0 End Function
Copy and paste the above function into the Standard Module of your database. You can try the function by calling it from the Debug Window with a folder name as shown below:
FolderCreation "C:\MyProjects"
VBScript Function: FolderDeletion().
After the sample run, use Windows Explorer to look for the folder name c:\MyProjects. The following VB Script Function FolderDeletion() can be used for removing a folder:
Public Function FolderDeletion(ByVal strFolder As String) Dim FSysObj, fldr On Error Resume Next Set FSysObj = CreateObject("Scripting.FileSystemObject") Set fldr = FSysObj.GetFolder(strFolder) If Err = 76 Then MsgBox "Folder: '" & strFolder & "' No found!" Else If MsgBox("Delete Folder: '" & strFolder & "' Proceed...?", vbOKCancel + vbDefaultButton2 + vbQuestion, "FolderDeletion()") = vbNo Then GoTo FolderDeletion_Exit Else fldr.Delete 'call the Delete Method of the Folder Object MsgBox "Folder: '" & strFolder & "' Deleted." End If End If FolderDeletion_Exit: On Error GoTo 0 End Function
Copy and paste the above code into the Standard Module of your database. You can run the above code either from the Debug Window or call it from a Command Button Click Event Procedure.
Sample Run from Debug Window:
FolderDeletion "C:\MyProjects"
OR
Private Sub cmdRun_Click() FolderDeletion txtFolderPath End Sub
No comments:
Post a Comment
Comments subject to moderation before publishing.