Can an Ms Access database create a backup of itself while it's open using vba?

后端 未结 3 1946
迷失自我
迷失自我 2020-12-07 04:51

Background: I have a process that links Quickbooks to Ms Access. If a button is pressed, some information will be queried from Quickbooks and then updates M

3条回答
  •  心在旅途
    2020-12-07 05:07

    The users "save as" does a different thing than just copying a file, it actually creates a new database, and exports everything to it. You can do the same if you wish (if there are no locked records), but it does require some coding.

    The "backup database" is unavailable from the save as menu if the file is opened by other users (and closes all open objects when used).

    You can, of course, create a new file, and then iterate through all tables, queries, forms, reports, macros and modules to copy them, and then iterate through all relationships to add them to the copy. Then you can copy all database properties to the new database. But that requires some work.

    See the following code to create a backup that ignores relationships and database properties

    Public Sub BackupDatabase(newLocation As String)
        'Make sure there isn't already a file with the name of the new database
        If Dir(newLocation) <> "" Then Kill newLocation
        'Create a new database using the default workspace
        'dbVersion30 = Jet 3, dbVersion40 = Jet4, dbVersion120 = 2007 accdb, dbVersion150 = 2013 accdb
        DBEngine.Workspaces(0).CreateDatabase newLocation, dbLangGeneral, Option:=dbVersion150
    
        'Iterate through common object collections, put the files in
        Dim iterator As Variant
        For Each iterator In CurrentDb.TableDefs
            If Not iterator.Name Like "MSys*" Then
                DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acTable, iterator.Name, iterator.Name
            End If
        Next iterator
        For Each iterator In CurrentDb.QueryDefs
            If Not iterator.Name Like "~sq_*" Then
                DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acQuery, iterator.Name, iterator.Name
            End If
        Next iterator
        For Each iterator In CurrentProject.AllForms
             DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acForm, iterator.Name, iterator.Name
        Next iterator
        For Each iterator In CurrentProject.AllReports
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acReport, iterator.Name, iterator.Name
        Next iterator
        For Each iterator In CurrentProject.AllMacros
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acMacro, iterator.Name, iterator.Name
        Next iterator
        For Each iterator In CurrentProject.AllModules
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acModule, iterator.Name, iterator.Name
        Next iterator
    End Sub
    

    Note that, depending on your security settings, you might get a lot of security popups.

提交回复
热议问题