Auto Backup of Split Database. (1 Viewer)

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 19:41
Joined
Apr 1, 2019
Messages
731
Hi, I wish to automatically (say on close of the database) backup both the FE & BE. I've seen some code but am unsure where to start. Appreciate any advice. Cheers.
 

GinaWhipp

AWF VIP
Local time
Today, 02:41
Joined
Jun 21, 2011
Messages
5,899
Why?

If the BE is on a Server then it gets backed up regularly, correct? And you should always have a master FE that can serve as a back up. Because you really don't want to attempt this if there is more than one person using the BE as that will quickly lead to errors and corruption.

Hmm, perhaps telling us why you think this important will help us understand.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 19:41
Joined
Apr 1, 2019
Messages
731
GinaWhipp, you are correct. Upon reflection there is clearly no need for me to backup the FE-Master. The DBguys suggested code looks ideal for backing up the BE. I'll just need to figure a method of triggering it. Probably a button for now, but I'd be interested in other suggestions. Thanks.
 

GinaWhipp

AWF VIP
Local time
Today, 02:41
Joined
Jun 21, 2011
Messages
5,899
Hmm, still wondering why you think the BE needs backing up. Doesn't the Server get backed up?
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 19:41
Joined
Apr 1, 2019
Messages
731
GinaWhipp, The server is regularly baked up. I guess I'm concerned about what to do if a BE table became corrupted. Is this something I should be concerned about?
 

GinaWhipp

AWF VIP
Local time
Today, 02:41
Joined
Jun 21, 2011
Messages
5,899
Well, IF that happens then the most you should loss is a day depending upon whether there are incremental back-ups during the day.

IMHO, you have more chance of corrupting your own BE by having it back-up via some button. Too many things could go wrong.

I would check with IT and make sure the Server is backed up regularly and go from there. You can even ask about mirroring which some companies do which means you MIGHT lose a day but probably more like an hour.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 19:41
Joined
Apr 1, 2019
Messages
731
GinaWhip, Excellent advice. Appreciate it. I agree, why introduce a BU routine that may in it'self cause a potential problem. Thanks.
 

GinaWhipp

AWF VIP
Local time
Today, 02:41
Joined
Jun 21, 2011
Messages
5,899
You're welcome!

And remember, we're here in case you don't get a satisfactory reason from IT. :D
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 02:41
Joined
Feb 19, 2002
Messages
43,263
You can ask your IT administrator to back up the BE multiple times per day.

What is critical though is backing up as you are doing development work. All my apps open to a logon form or a menu and that form is loaded all the while the application is open so that form is the last one to close before the app shuts down. I have code in the app that asks me (it checks login id) if I want to create a back up if it determines that any object changed while the app was open. That way I have inter day backups which make it much easier to recover from accidents. I do two types of back up. One is just the full file and the other exports every object (except tables) as text to a folder labeled with the date and time. I clean these up once a month so the number of them doesn't get overwhelming.

If that is of interest, let me know and I'll post some code.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 19:41
Joined
Apr 1, 2019
Messages
731
Pat, I've discovered the importance of regular back ups when developing an app. I currently do this manually but automating would be real neat. If you were prepared to publish your code with suitable recognition i'd bet others would also find it useful. Thanks
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 16:41
Joined
Jan 20, 2009
Messages
12,852
Backing up Access while users are connected can be problematic.

If the data is critical consider changing the backend to a database server (eg MSSQL Server) which have much better facilities for backup and recovery.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 19:41
Joined
Apr 1, 2019
Messages
731
Galaxiom, i do have code that displays users. I'll make sure that i check before doing any kind of backup. Thanks
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 02:41
Joined
Feb 19, 2002
Messages
43,263
This is the code that goes in the Unload event of the form that opens first and is unloaded last. Some of the paths are hardcoded in this example but I suggest using a different method. You could also put the user names to check for in a table so you don't have to embed them in the code either.

The code uses FSO (File System Object) so you MUST add a reference to Office xx Object library. Open any module in design view and on the code page pick tools/references to add the reference.
Code:
Private Sub Form_Unload(Cancel As Integer)
    If Environ("username") = "phartman" Or Environ("username") = "Pat" Then
        If MsgBox("Save as text?", vbYesNo) = vbYes Then
            Call ExportOnClose
        End If
    End If
    
End Sub

This code goes in a common utility module that you include in ALL your databases.
Code:
Option Compare Database
Option Explicit
    Public iCountForms As Integer
    Public iCountReports As Integer
    Public iCountQueries As Integer
    Public iCountModules As Integer
    Public iCountScripts As Integer    'macros
Public Sub ExportOnClose()

'''REQUIRES reference to Office xx Object Library

    Dim strPath As String
    Dim objFSO As Object
    Dim strMsg As String

On Error GoTo ErrProc
    
    'create new folder
    
    strPath = "C:\Pat\RAS\TextFiles" & Format(Date, "yymmdd")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Not objFSO.FolderExists(strPath) Then
        objFSO.CreateFolder (strPath)
    Else
        strPath = strPath & "A"
        If Not objFSO.FolderExists(strPath) Then
            objFSO.CreateFolder (strPath)
        Else
            strPath = InputBox("enter path please")
            objFSO.CreateFolder (strPath)
        End If
    End If
    'export all objects
    MsgBox "This may take a few minutes.  Please wait for the count message.", vbOKOnly
    Call ExportDatabaseObjects("forms", strPath)
    Call ExportDatabaseObjects("reports", strPath)
    Call ExportDatabaseObjects("modules", strPath)
    Call ExportDatabaseObjects("QueryDefs", strPath)
    Call ExportDatabaseObjects("scripts", strPath)
    strMsg = "Exported Forms = " & iCountForms & vbCrLf
    strMsg = strMsg & "         Reports = " & iCountReports & vbCrLf
    strMsg = strMsg & "         Modules = " & iCountModules & vbCrLf
    strMsg = strMsg & "         Queries = " & iCountQueries & vbCrLf
    strMsg = strMsg & "         Macros = " & iCountScripts & vbCrLf
    Debug.Print strMsg
    MsgBox strMsg, vbOKOnly
    
ExitProc:
    Exit Sub
ErrProc:
    Select Case Err.Number
        Case Else
            MsgBox Err.Number & "--" & Err.Description
    End Select
End Sub


Public Sub ExportDatabaseObjects(ExportType As String, Optional ExpLoc As Variant)
On Error GoTo Err_ExportDatabaseObjects
    
    
    Dim db As DAO.Database
    Dim td As DAO.TableDef
    Dim D As Document
    Dim C As Container
    Dim i As Integer
    Dim sExportLocation As String
    
    Set db = CurrentDb()
''import from text =
''application.Application.LoadFromText acForm, "frmRisks","C:\Temp\TextRiskReview070615\Form_frmRisks.txt"

    If ExpLoc & "" = "" Then
        sExportLocation = "C:\Pat\RAS\TextFiles\" 'Do not forget the closing back slash! ie: C:\Temp\
    Else
        sExportLocation = ExpLoc
    End If
    If Right(sExportLocation, 1) = "\" Then
    Else
        sExportLocation = sExportLocation & "\"
    End If
    Select Case ExportType
        Case "TableDefs"
            For Each td In db.TableDefs 'Tables
                If Left(td.Name, 4) <> "MSys" Then
                    DoCmd.TransferText acExportDelim, , td.Name, sExportLocation & "Table_" & td.Name & ".txt", True
                End If
            Next td
        Case "Forms"
            Set C = db.Containers("Forms")
            iCountForms = 0
            For Each D In C.Documents
                Application.SaveAsText acForm, D.Name, sExportLocation & "Form_" & D.Name & ".txt"
                iCountForms = iCountForms + 1
            Next D
        Case "Reports"
            Set C = db.Containers("Reports")
            iCountReports = 0
            For Each D In C.Documents
                Application.SaveAsText acReport, D.Name, sExportLocation & "Report_" & D.Name & ".txt"
                iCountReports = iCountReports + 1
            Next D
        Case "Scripts"
            Set C = db.Containers("Scripts")
            iCountScripts = 0
            For Each D In C.Documents
                Application.SaveAsText acMacro, D.Name, sExportLocation & "Macro_" & D.Name & ".txt"
                iCountScripts = iCountScripts + 1
            Next D
        Case "Modules"
            Set C = db.Containers("Modules")
            iCountModules = 0
            For Each D In C.Documents
                Application.SaveAsText acModule, D.Name, sExportLocation & "Module_" & D.Name & ".txt"
                iCountModules = iCountModules + 1
            Next D
        Case "QueryDefs"
            iCountQueries = 0
            For i = 0 To db.QueryDefs.Count - 1
                Application.SaveAsText acQuery, db.QueryDefs(i).Name, sExportLocation & "Query_" & db.QueryDefs(i).Name & ".txt"
                iCountQueries = iCountQueries + 1
            Next i
        Case Else
    End Select

    Set db = Nothing
    Set C = Nothing
    
    'MsgBox "All database objects have been exported as a text file to " & sExportLocation, vbInformation
    
Exit_ExportDatabaseObjects:
    Exit Sub
    
Err_ExportDatabaseObjects:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_ExportDatabaseObjects
    
End Sub

This is code you can use to export/import a single object:
Code:
Public Sub ImportForm()
    Application.Application.LoadFromText acForm, "sfrmReconcilePayments", "C:\Pat\RAS\TextFiles120926A\Form_sfrmReconcilePayments.txt"
End Sub
Public Sub ExportForm()
    Application.SaveAsText acForm, "frmReconcilePayments", "C:\Pat\RAS\TextFiles120927A\Form_frmReconcilePaymentsOLD.txt"
End Sub
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 19:41
Joined
Apr 1, 2019
Messages
731
Pat, thanks a lot. Cheers.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 02:41
Joined
Feb 19, 2002
Messages
43,263
You're welcome. This code has saved my bacon more than once :)
 

Users who are viewing this thread

Top Bottom