Setup Form with Backup Button - Help!

Sorry I took the code out and made it a Module a while back. the code I gave was from an updates form.

The code goes in On Open.

Make the form as above. Then create a Module and use the following code:
Code:
Option Compare Database
Option Explicit

Function Restore()
Dim str As String
Dim buf As String
Dim MD_Date As Variant
Dim fs As Object
Dim source As String
Dim pubInputFolder As String
Dim txtfolder As String
Dim deletesource As String

Const conPATH_FILE_ACCESS_ERROR = 75

    On Error GoTo 0
    
    With Application.FileDialog(4)
        .InitialFileName = CurrentProject.Path & "\Backups\"
        .AllowMultiSelect = False
        .Filters.Clear
        .Show
        pubInputFolder = .SelectedItems(1)
        txtfolder = pubInputFolder

    End With

On Error GoTo Backup

    buf = CurrentProject.Path & "\Backups\"
    MkDir buf

Resume Backup

Backup:

    MD_Date = Format(Date, "yyyy-mm-dd ") & Format(Time, "hh-mm-ss") & " R"

        str = CurrentProject.Path & "\Backups\" & MD_Date
        source = CurrentProject.Path & "\Data\"

    MkDir str

    Set fs = CreateObject("Scripting.FileSystemObject")
        fs.CopyFile source & "*.accdb", str
    Set fs = Nothing

    deletesource = CurrentProject.Path & "\Data\"

    Set fs = CreateObject("Scripting.FileSystemObject")
        fs.DeleteFile deletesource & "\*.accdb"
    Set fs = Nothing

Dim backupsource As String
Dim destination As String

    backupsource = txtfolder
    destination = CurrentProject.Path & "\Data\"

    Set fs = CreateObject("Scripting.FileSystemObject")
        fs.CopyFile backupsource & "\*.*", destination & "\"
    Set fs = Nothing

MsgBox "Data from " & vbNewLine & backupsource & vbNewLine & _
        "successfully restored!", vbInformation, "Restore Successful"

    DoCmd.Close acForm, "F_0_Restore"

ExitBackup:

    DoCmd.OpenForm "_Splash1", acNormal, "", "", , acNormal
    
  Exit Function

ErrBackup:

  If Err.Number = conPATH_FILE_ACCESS_ERROR Then
    MsgBox "The following Path, " & str & ", already exists or there was an Error " & _
           "accessing it!", vbExclamation, "Path/File Access Error"
  Else
    MsgBox Err.Description, vbExclamation, "Error Creating " & str
  
    Resume ExitBackup
End If
End Function
 

Attachments

  • 1.zip
    1.zip
    426.7 KB · Views: 260
Last edited:
Backup Button
Code:
Private Sub Button_Backup_Click()
Dim str As String
Dim buf As String
Dim MD_Date As Variant
Dim fs As Object
Dim source As String
Const conPATH_FILE_ACCESS_ERROR = 75
On Error GoTo Backup_Button_Backup
'buf = Back Up Folder
'buf is created if it does not exist
'CurrentProject.Path = the path that the FE is located
buf = CurrentProject.Path & "\Backups\"
MkDir buf
    Resume Backup_Button_Backup
Backup_Button_Backup:
'Use yyyy-mm-dd hh-mm-ss as folder name. Change as needed.
MD_Date = Format(Date, "yyyy-mm-dd ") & Format(Time, "hh-mm-ss")
str = CurrentProject.Path & "\Backups\" & MD_Date
'Source = where the data is stored
source = CurrentProject.Path & "\Data\"
MkDir str
Set fs = CreateObject("Scripting.FileSystemObject")
'Change the file extension as needed
[U][COLOR=DeepSkyBlue]fs.CopyFile source & "*.accdb", str[/COLOR][/U]
Set fs = Nothing
MsgBox "Data backup at " & vbCrLf & MD_Date & vbCrLf & "successfully!", _
        vbInformation, "Backup Successful"
Exit_Button_Backup:
  Exit Sub
Err_Button_Backup:
  If err.Number = conPATH_FILE_ACCESS_ERROR Then
    MsgBox "The following Path, " & str & ", already exists or there was an Error " & _
           "accessing it!", vbExclamation, "Path/File Access Error"
  Else
    MsgBox err.Description, vbExclamation, "Error Creating " & str
  
End If
    Resume Exit_Button_Backup
End Sub

Sir I need your help regarding your code

Runtime error 76
path not found

fs.CopyFile source & "*.accdb", str

Error in this field..hope you can help me,
Im using access 2010
 
What extension are you using for your backend? It should be ACCDB. If it is something else, change the code to reflect that.

fs.CopyFile source & "*.accdb", str

Also the file to backup must be in a folder DATA in the project folder:

source = CurrentProject.Path & "\Data\"
 

Users who are viewing this thread

Back
Top Bottom