This article shows how to use VBA in Access 2013 to delete an existing directory and its contents, using the FileSystemObject and the DeleteFolder method.
Visual Basic for Applications (VBA) is a programming language that allows you to automate tasks and customize the functionality of Microsoft Office applications, such as Access 2013. One of the common tasks that you may need to perform with VBA is to delete an existing directory and its contents, such as files and subfolders.
In this article, we will explain how to use VBA in Access 2013 to delete an existing directory and its contents, using the FileSystemObject and the DeleteFolder method. We will also provide some examples and tips to help you avoid errors and handle exceptions.
Table of Contents
- What is the FileSystemObject?
- How to Use the DeleteFolder Method?
- Examples and Tips
- Example 1: Delete a folder if it exists
- Example 2: Delete multiple folders that match a pattern
- Example 3: Delete read-only folders
- Tip 1: Handle errors and exceptions
- Tip 2: Use the Application.FileDialog object to select a folder
- Frequently Asked Questions (FAQs)
- Summary
What is the FileSystemObject?
The FileSystemObject is an object that provides access to the file system of the computer. It allows you to create, read, write, delete, and manipulate files and folders. You can use the FileSystemObject in VBA by adding a reference to the Microsoft Scripting Runtime library in the Tools > References menu of the Visual Basic Editor.
The FileSystemObject has several methods and properties that you can use to work with files and folders. One of the methods that we will use in this article is the DeleteFolder method, which deletes one or more folders and their contents.
How to Use the DeleteFolder Method?
The DeleteFolder method has the following syntax:
FileSystemObject.DeleteFolder Source, Force
The parameters of the DeleteFolder method are:
- Source: The name and path of the folder or folders to delete. You can use wildcards (*) to specify multiple folders that match a pattern. For example, C:\TestDir* will delete all folders that start with TestDir in the C: drive.
- Force: An optional boolean value that specifies whether to delete folders that have the read-only attribute set. The default value is False, which means that read-only folders will not be deleted. If you set this value to True, read-only folders will be deleted.
To use the DeleteFolder method, you need to create an instance of the FileSystemObject and assign it to a variable. Then, you can call the DeleteFolder method on the variable and pass the parameters. For example, the following code will delete the folder C:\TestDir and its contents:
Dim FSO As New FileSystemObject
FSO.DeleteFolder "C:\TestDir", False
Examples and Tips
Here are some examples and tips on how to use the DeleteFolder method in different scenarios.
Example 1: Delete a folder if it exists
Before deleting a folder, you may want to check if it exists to avoid errors. You can use the FolderExists method of the FileSystemObject to check if a folder exists. The FolderExists method returns True if the folder exists and False otherwise. For example, the following code will check if the folder C:\TestDir exists and delete it if it does:
Dim FSO As New FileSystemObject
If FSO.FolderExists("C:\TestDir") Then
FSO.DeleteFolder "C:\TestDir", False
End If
Example 2: Delete multiple folders that match a pattern
You can use wildcards (*) to delete multiple folders that match a pattern. For example, the following code will delete all folders that start with TestDir in the C: drive:
Dim FSO As New FileSystemObject
FSO.DeleteFolder "C:\TestDir*", False
Example 3: Delete read-only folders
By default, the DeleteFolder method will not delete folders that have the read-only attribute set. If you want to delete read-only folders, you need to set the Force parameter to True. For example, the following code will delete the folder C:\TestDir and its contents, even if it is read-only:
Dim FSO As New FileSystemObject
FSO.DeleteFolder "C:\TestDir", True
Tip 1: Handle errors and exceptions
When using the DeleteFolder method, you may encounter errors and exceptions, such as:
- The folder does not exist
- The folder is in use by another process
- The folder is protected by the system
- The user does not have permission to delete the folder
To handle these errors and exceptions, you can use the On Error statement and the Err object in VBA. The On Error statement allows you to specify what to do when an error occurs, such as resume the next line of code, go to a label, or end the procedure. The Err object contains information about the error, such as the number, the description, and the source.
For example, the following code will try to delete the folder C:\TestDir and its contents, and display a message box with the error information if an error occurs:
Dim FSO As New FileSystemObject
On Error GoTo ErrorHandler
FSO.DeleteFolder "C:\TestDir", False
Exit Sub
ErrorHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description & " (" & Err.Source & ")"
Tip 2: Use the Application.FileDialog object to select a folder
If you want to allow the user to select a folder to delete, you can use the Application.FileDialog object in VBA. The Application.FileDialog object allows you to display a dialog box that lets the user choose a file or a folder. You can use the msoFileDialogFolderPicker constant to specify that the dialog box should display folders only.
For example, the following code will display a dialog box that allows the user to select a folder to delete, and then delete the folder and its contents:
Dim FSO As New FileSystemObject
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.Title = "Select a folder to delete"
fd.AllowMultiSelect = False
If fd.Show = -1 Then
FSO.DeleteFolder fd.SelectedItems(1), False
End If
Frequently Asked Questions (FAQs)
Question: What is VBA?
Answer: VBA stands for Visual Basic for Applications, which is a programming language that allows you to automate tasks and customize the functionality of Microsoft Office applications, such as Access 2013.
Question: What is the FileSystemObject?
Answer: The FileSystemObject is an object that provides access to the file system of the computer. It allows you to create, read, write, delete, and manipulate files and folders.
Question: What is the DeleteFolder method?
Answer: The DeleteFolder method is a method of the FileSystemObject that deletes one or more folders and their contents.
Question: How to use the DeleteFolder method?
Answer: To use the DeleteFolder method, you need to create an instance of the FileSystemObject and assign it to a variable. Then, you can call the DeleteFolder method on the variable and pass the parameters. The parameters are the name and path of the folder or folders to delete, and an optional boolean value that specifies whether to delete read-only folders.
Question: How to handle errors and exceptions when using the DeleteFolder method?
Answer: To handle errors and exceptions when using the DeleteFolder method, you can use the On Error statement and the Err object in VBA. The On Error statement allows you to specify what to do when an error occurs, such as resume the next line of code, go to a label, or end the procedure. The Err object contains information about the error, such as the number, the description, and the source.
Summary
In this article, we have explained how to use VBA in Access 2013 to delete an existing directory and its contents, using the FileSystemObject and the DeleteFolder method. We have also provided some examples and tips to help you avoid errors and handle exceptions. We hope that this article has helped you to understand and use the DeleteFolder method in VBA.
Disclaimer: This article is for informational purposes only and does not constitute professional advice. The author and the publisher are not liable for any damages or losses that may result from the use of the information or tools in this article. The user is responsible for verifying the accuracy and validity of the information and tools before applying them to their own situation. The user is also responsible for complying with any applicable laws and regulations when using the information or tools in this article.