Ms Access 数据库可以在使用 vba 打开时创建自己的备份吗? [英] Can an Ms Access database create a backup of itself while it's open using vba?

查看:44
本文介绍了Ms Access 数据库可以在使用 vba 打开时创建自己的备份吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我有一个将 Quickbooks 链接到 Ms Access 的流程.如果按下按钮,将从 Quickbooks 查询一些信息,然后更新 Ms Access.如果停电,或者用户在同步过程中强制关闭Ms Access,可能会导致部分信息损坏.

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 Ms Access. If the power goes out, or if the user forces Ms Access to close during the sync process, it can cause some of the information to be corrupted.

目标:我想在表单上有一个按钮,用户可以按下它,它将当前数据库保存到预定义的位置,并在文件名中附加日期和时间.

Goal: I want to have a button on a form that users can press and it'll save the current database to a predefined location with the date and time attached to the filename.

我一直在阅读如何备份其他关闭的数据库(使用 FileCopy),但是您需要一个 hacky-workaround 解决方案来在打开的数据库上执行此操作,这可能会导致数据损坏.我并不完全相信,因为用户可以随时使用另存为".

I keep reading how it's possible to backup other closed databases (using FileCopy), but you need a hacky-workaround solution to do it on an open database, which can lead to data corruption. I'm not entirely convinced since the user can use "Save As" at any time.

有没有办法备份当前打开的 Ms Access 数据库,或者可以满足我的需求?

Is there a way to backup a currently open Ms Access database, or something that will fulfill my needs?

推荐答案

用户另存为"的作用与仅仅复制文件不同,它实际上创建了一个新数据库,并将所有内容导出到其中.如果您愿意,您也可以这样做(如果没有锁定的记录),但它确实需要一些编码.

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.

这篇关于Ms Access 数据库可以在使用 vba 打开时创建自己的备份吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆