Public Function BackupFEDatabase()
On Error GoTo Err_Handler
'creates a copy of the current db (frontend) to the specified backups folder with date/time suffix
Dim fso As Object
Dim strOldPath As String, strNewPath As String, strTempPath As String, strFileSize As String
Dim newlength As Long
Set fso = CreateObject("Scripting.FileSystemObject")
strFileType = Mid(CurrentDb.Name, InStr(CurrentDb.Name, ".")) 'e.g. .accdb
strFilename = Mid(CurrentDb.Name, InStrRev(CurrentDb.Name, "\") + 1)
strFilename = Left(strFilename, Len(strFilename) - Len(strFileType)) 'e.g. JSONTest
strOldPath = CurrentDb.Name
strTempPath = Left(CurrentDb.Name, InStr(CurrentDb.Name, ".") - 1) & _
"_TEMP" & Mid(CurrentDb.Name, InStr(CurrentDb.Name, "."))
'CR - the code below refers to a function GetBackupsFolder
'Replace with your own folder name or function
strNewPath = GetBackupsFolder & "\FE\" & _
strFilename & "_" & Format(Now, "yyyymmddhhnnss") & strFileType
If MsgBox("This procedure is used to make a backup copy of the Access front end (FE) database. " & vbCrLf & _
"The backup will be saved to the Backups folder with date/time suffix " & vbCrLf & _
vbTab & "e.g. " & strNewPath & " " & vbCrLf & vbCrLf & _
"This can be used for recovery in case of problems " & vbCrLf & vbCrLf & _
"Create a backup now?", _
vbExclamation + vbYesNo, "Copy the Access FE database?") = vbNo Then
Exit Function
Else
'copy database to a temp file
fso.CopyFile strOldPath, strTempPath
Set fso = Nothing
strNewPath = GetBackupsFolder & "\FE\" & _
strFilename & "_v" & GetVersion() & "_" & Format(Now, "yyyymmddhhnnss") & strFileType
'Debug.Print strTempPath
'Debug.Print strNewPath
'compact the temp file
DBEngine.CompactDatabase strTempPath, strNewPath
'delete the tempfile
Kill strTempPath
DoEvents
'get size of backup (this can be omitted if not wanted)
newlength = FileLen(strNewPath) 'in bytes
'setup string to display file size
If FileLen(strNewPath) < 1024 Then 'less than 1KB
strFileSize = newlength & " bytes"
ElseIf FileLen(strNewPath) < 1024 ^ 2 Then 'less than 1MB
strFileSize = Round((newlength / 1024), 0) & " KB"
ElseIf newlength < 1024 ^ 3 Then 'less than 1GB
strFileSize = Round((newlength / 1024), 0) & " KB (" & Round((newlength / 1024 ^ 2), 1) & " MB)"
Else 'more than 1GB
strFileSize = Round((newlength / 1024), 0) & " KB (" & Round((newlength / 1024 ^ 3), 2) & " GB)"
End If
DoEvents
End If
MsgBox "The Access FE database has been successfully backed up. " & vbCrLf & _
"The backup file is called " & vbCrLf & _
vbTab & strNewPath & " " & vbCrLf & vbCrLf & _
"The file size is " & strFileSize, vbInformation, "Access FE Backup completed"
Exit_Handler:
Exit Function
Err_Handler:
Set fso = Nothing
If Err <> 0 Then
MsgBox "Error " & Err.Number & " in BackupFEDatabase procedure : " & vbCrLf & _
Err.description, vbCritical, "Error copying database"
End If
Resume Exit_Handler
End Function