Trong VBA trong Access, bạn có thể xử lý file và thư mục (copy, xóa, tạo, kiểm tra tồn tại...) bằng cách kết hợp các hàm nội bộ và thư viện Scripting.FileSystemObject. Dưới đây là hướng dẫn đầy đủ:
Để xử lý file nâng cao, bạn nên sử dụng Scripting.FileSystemObject.
👉 Cách khai báo:
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")🔹 Kiểm tra file có tồn tại:
If fso.FileExists("C:\Data\myfile.txt") Then
    MsgBox "File tồn tại!"
End If🔹 Kiểm tra folder có tồn tại:
If fso.FolderExists("C:\Data") Then
    MsgBox "Folder tồn tại!"
End IfIf Not fso.FolderExists("C:\NewFolder") Then
    fso.CreateFolder "C:\NewFolder"
End IfDim folder As Object
Dim file As Object
Set folder = fso.GetFolder("C:\Data")
For Each file In folder.Files
    Debug.Print file.Name
Next Sub BackupAccessFile()
    Dim fso As Object
    Dim sourceFile As String, backupFile As String
    sourceFile = "C:\Data\MyApp.accdb"
    backupFile = "D:\Backup\MyApp_Backup.accdb"
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(sourceFile) Then
        fso.CopyFile sourceFile, backupFile, True
        MsgBox "Đã sao lưu thành công!"
    Else
        MsgBox "Không tìm thấy file nguồn!"
    End If
End Sub Tìm kiếm:
Hướng dẫn chi tiết cách xử lý file và thư mục trong VBA, qua các bài ví dụ thực hành làm phần mềm thực tế bằng MS Access