Opening Access database in exclusive mode

旧街凉风 提交于 2020-01-13 17:57:25

问题


I'd like to be able to write a script that opens a Access database in exclusive mode so I can refresh the information in it without worrying about other users accessing the database in an inconsistent state. Is there a way to do this using VBA, or through a COM interface using VBScript?


回答1:


According to this table of OLEDB init properties, you should add a "Mode=Share Exclusive" to your connection string.




回答2:


I didn't know what should happen if any users have the database open when your script starts. So I chose to check for the presence of a database lock file, and only continue if the lock file doesn't exist.

Here is 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


来源:https://stackoverflow.com/questions/5792169/opening-access-database-in-exclusive-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!