用户“另存为”的作用与复制文件不同,它实际上创建了一个新数据库,并将所有内容导出到其中。如果您愿意(如果没有锁定的记录),您也可以这样做,但它确实需要一些编码。
如果文件被其他用户打开(并在使用时关闭所有打开的对象),则另存为菜单中的“备份数据库”不可用。
当然,您可以创建一个新文件,然后遍历所有表、查询、表单、报表、宏和模块以复制它们,然后遍历所有关系以将它们添加到副本中。然后您可以将所有数据库属性复制到新数据库。但这需要一些工作。
请参阅以下代码以创建忽略关系和数据库属性的备份
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
请注意,根据您的安全设置,您可能会收到很多安全弹出窗口。