用戶「另存爲」與複製文件不同,它實際上創建了一個新的數據庫,並將所有內容導出到該文件中。如果你願意,你可以做同樣的事情(如果沒有鎖定的記錄),但它確實需要一些編碼。
如果文件被其他用戶打開(並在使用時關閉所有打開的對象),則「備份數據庫」在「另存爲」菜單中不可用。
當然,您可以創建一個新文件,然後遍歷所有表,查詢,表單,報表,宏和模塊以複製它們,然後遍歷所有關係以將它們添加到副本。然後,您可以將所有數據庫屬性複製到新數據庫。但這需要一些工作。
請看下面的代碼來創建忽略的關係和數據庫屬性
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
注意,根據您的安全設置,你可能會得到很多安全彈出窗口的備份。
我正在尋找一種方法來備份我的表及其信息。由於您的解決方案確實如此,我非常感興趣。 – Jaitnium
查看當前的編輯。從來沒有問題,但沒有做過廣泛的測試 –
我有幾個鏈接表,我想避免複製,因爲他們需要打開Quickbooks。我應該只檢查表名的iterator.Name跳過這些表嗎? – Jaitnium