2011-04-26 40 views
2

我希望能夠編寫一個以獨佔模式打開Access數據庫的腳本,這樣我就可以刷新其中的信息,而不用擔心其他用戶訪問處於不一致狀態的數據庫。有沒有辦法使用VBA或通過使用VBScript的COM接口來執行此操作?以獨佔模式打開Access數據庫

+3

不要忘記堅持所有用戶追了出去。 – 2011-04-26 14:56:18

回答

3

我不知道如果有任何用戶在腳本啓動時打開數據庫會發生什麼情況。所以我選擇檢查是否存在數據庫鎖定文件,並且只有在鎖定文件不存在時纔會繼續。

這裏是DoSomethingExclusively.vbs:

Option Explicit 

Dim strFolder 
Dim strMdb 
Dim strLckFile 
Dim objFSO 
Dim appAccess 

strFolder = "C:\Access\webforums\" 
strMdb = "whiteboard2003.mdb" 
strLckFile = "whiteboard2003.ldb" 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
If Not (objFSO.FileExists(strFolder & strLckFile)) Then 
    Set appAccess = CreateObject("Access.Application") 
    appAccess.OpenCurrentDatabase strFolder & strMdb, True 
    '* do something here; this just adds a row with current time *' 
    appAccess.CurrentDb.Execute _ 
     "INSERT INTO foo (bar) VALUES ('" & CStr(Now()) & "');" 
    appAccess.Quit 
    Set appAccess = Nothing 
End If 
Set objFSO = Nothing 
+0

謝謝,這很有幫助。我希望我可以將這兩個答案標記爲已接受的答案。 – Tmdean 2011-04-26 15:56:39

+0

我很確定你不能使用任何帶有OLEDB連接的DAO東西。你必須使用ADO。 – Tmdean 2011-04-26 16:23:52

+0

檢查LDB文件的存在是數據庫是否打開的不好代理,因爲LDB文件可以在數據庫關閉時存活(例如,當Access崩潰時)。 – 2011-04-29 03:49:45

相關問題