2017-08-16 24 views
2

背景:我有一個將Quickbooks鏈接到Ms Access的過程。如果按下某個按鈕,將從Quickbooks中查詢一些信息,然後更新Ms Access。如果電源關閉,或者用戶強制Ms Access在同步過程中關閉,則可能會導致一些信息被破壞。Ms Access數據庫在使用vba打開時是否可以創建自己的備份?

目標:我想要一個表單上的按鈕,用戶可以按下,它會將當前數據庫保存到預定義的位置,日期和時間附加到文件名。

我繼續讀它是如何可能的其他備份關閉數據庫(使用FileCopy),但你需要一個哈克 - 解決方法解決方案做一個打開的數據庫,這可能會導致數據損壞的。我並不完全相信,因爲用戶可以隨時使用「另存爲」。

有沒有辦法備份當前打開的Ms Access數據庫,或者是否能滿足我的需求?

回答

2

用戶「另存爲」與複製文件不同,它實際上創建了一個新的數據庫,並將所有內容導出到該文件中。如果你願意,你可以做同樣的事情(如果沒有鎖定的記錄),但它確實需要一些編碼。

如果文件被其他用戶打開(並在使用時關閉所有打開的對象),則「備份數據庫」在「另存爲」菜單中不可用。

當然,您可以創建一個新文件,然後遍歷所有表,查詢,表單,報表,宏和模塊以複製它們,然後遍歷所有關係以將它們添加到副本。然後,您可以將所有數據庫屬性複製到新數據庫。但這需要一些工作。

請看下面的代碼來創建忽略的關係和數據庫屬性

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 

注意,根據您的安全設置,你可能會得到很多安全彈出窗口的備份。

+0

我正在尋找一種方法來備份我的表及其信息。由於您的解決方案確實如此,我非常感興趣。 – Jaitnium

+0

查看當前的編輯。從來沒有問題,但沒有做過廣泛的測試 –

+0

我有幾個鏈接表,我想避免複製,因爲他們需要打開Quickbooks。我應該只檢查表名的iterator.Name跳過這些表嗎? – Jaitnium

0

您可以使用下面的代碼行,這是假設你有一個拆分數據庫:

Public Sub CompactDB() 
dim strFrom as string 
dim strTo as string 

strFrom = "C:\Your Database Location Including File Name and Extension" 
strTo = "C:\Your new Database backup location File Name and Extension" 

DBEngine.CompactDatabase strFrom, strTo 

End Sub 

注:這會不會壓縮您的當前後端(strFrom),這使得後端的副本位於strFrom到新位置(strTo)。

只需點擊按鈕或從另一個事件調用此子。

但是,我處理這個問題的方式是創建一個存儲2個字段的表。字段1被命名爲「DestinationFrom」,字段2被命名爲「DestinationTo」。然後,我存儲象下面這樣的記錄:

DestinationFrom = C:\當前後端的目標

DestinationTo = C:\備份目的地

然後使用下面的代碼:

Public sub CompactDB() 
dim rst as dao.recordset 
dim strSQL as string 
dim strLocation as string 
Dim strDestination as string 

strsql = "SELECT * " & _ 
     "FROM DestinationTable;" 
set rst = currentdb.openrecordset(strsql) 
strlocation = rst![DestinationFrom] 
strdestination = rst![DestinationTo] 
rst.close 
set rst = nothing 

DBEngine.CompactDatabase rst![DestinationFrom] , rst![DestinationTo] 

if not rst is nothing then 
rst.close 
set rst = nothing 
end if 
End Sub 

這樣,如果我的代碼永遠失敗導致文件夾被刪除或移動,我可以更改表中字段的字符串位置,而無需更改任何硬編碼並需要發佈新副本的內容。允許分割數據庫中的多個用戶時非常有用

+0

編輯第二個代碼,忘記了「之前SELECT – Chris

+0

這看起來很有前途。我試圖用它來備份當前後端到一個新的位置使用這一行代碼:DBEngine.CompactDatabase BackendPath(」tbl「),BackendPath(」tbl「 )&「_backup」然而,當這段代碼運行時,我得到一個運行時錯誤'3704':你試圖打開一個已經被用戶'Admin'在機器{Computer Name}上打開的數據庫,當數據庫但我真的希望這對我有效 – DRC

+0

BAH!我的錯誤,我編輯了我的第二個代碼來解決這個問題,問題是當第一次打開一個記錄集到strSQL查詢時,它創建了一個到數據庫。由於存在當前連接,數據庫無法壓縮並修復到目標位置。 – Chris

相關問題