ms asccess 2010 backend data base backup
Tutorials > Vba tools - Code
Compact and backup backend MS Access database
Public Sub BackUpAndCompactBE()
'Courtesy of datAdrenaline
'Costas notes >> Hidden means linked tables
On Error GoTo errHandler
Dim oFSO As Object
Dim strDestination As String
Dim strSource As String
'Get the source of your back end
strSource = Split(Split(CurrentDb.TableDefs("tblHidden").Connect, "Database=")(1), ";")(0)
'Determine your destination
strDestination = CurrentProject.Path & "\SCR_BE (" & Format(Now, "yyyymmddhhnnss") & ").accdb"
'Flush the cache of the current database
DBEngine.Idle
'Create a file scripting object that will backup the db
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CopyFile strSource, strDestination
Set oFSO = Nothing
'Compact the new file, ...
Name strDestination As strDestination & ".cpk"
DBEngine.CompactDatabase strDestination & ".cpk", strDestination
Kill strDestination & ".cpk"
'Notify users
MsgBox "Backup file '" & strDestination & "' has been created.", vbInformation, "Backup Completed!"
errExit:
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
Resume errExit
End Sub
'Courtesy of datAdrenaline
'Costas notes >> Hidden means linked tables
On Error GoTo errHandler
Dim oFSO As Object
Dim strDestination As String
Dim strSource As String
'Get the source of your back end
strSource = Split(Split(CurrentDb.TableDefs("tblHidden").Connect, "Database=")(1), ";")(0)
'Determine your destination
strDestination = CurrentProject.Path & "\SCR_BE (" & Format(Now, "yyyymmddhhnnss") & ").accdb"
'Flush the cache of the current database
DBEngine.Idle
'Create a file scripting object that will backup the db
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CopyFile strSource, strDestination
Set oFSO = Nothing
'Compact the new file, ...
Name strDestination As strDestination & ".cpk"
DBEngine.CompactDatabase strDestination & ".cpk", strDestination
Kill strDestination & ".cpk"
'Notify users
MsgBox "Backup file '" & strDestination & "' has been created.", vbInformation, "Backup Completed!"
errExit:
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
Resume errExit
End Sub