Backup current access file using vba while the file is open

jack1234

Registered User.
Local time
Today, 13:18
Joined
Jun 2, 2007
Messages
16
I have a button in the form of the current access file. What I want to do is when the user click on the button,
triggered the vba to backup current access file. But since the file is currently open, is this possible? If yes, may I know how to do it with vba?

I have tried two methods but failed:
1) Use copytofile method, but I get permission denied due to file open;
2) Use dbengine.compactdatabase method, but it also has problem if the file is open.
 
you cannot back up an access db (or any file) whilst it is still open.
 
I backup current ACCESS file (.mdb) with a command button and SHELL command to call a .bat file.
 
You can use the following code to back up a folder and all file within even if they are open.

Code:
Private Sub Command2_Click()
 
'This example copy all files and subfolders from FromPath to ToPath.
'Note: If ToPath already exist it will overwrite existing files in this folder
'if ToPath not exist it will be made for you.
    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    FromPath = "FULL FILE PATH"
    ToPath = "FULL FILE PATH\" & Format(Now, "yyyy-mm-dd h-mm-ss")
'If you want to create a backup of your folder every time you run this macro
'you can create a unique folder with a Date/Time stamp.
'ToPath = "C:\Users\Ron\" & Format(Now, "yyyy-mm-dd h-mm-ss")
    If Right(FromPath, 1) = "\" Then
        FromPath = Left(FromPath, Len(FromPath) - 1)
    End If
    If Right(ToPath, 1) = "\" Then
        ToPath = Left(ToPath, Len(ToPath) - 1)
    End If
    Set FSO = CreateObject("scripting.filesystemobject")
    If FSO.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If
    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub
 

Users who are viewing this thread

Back
Top Bottom