vba 以独占模式打开Access数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5792169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:06:05  来源:igfitidea点击:

Opening Access database in exclusive mode

ms-accessvbavbscript

提问by Tmdean

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?

我希望能够编写一个脚本,以独占模式打开 Access 数据库,这样我就可以刷新其中的信息,而不必担心其他用户以不一致的状态访问数据库。有没有办法使用 VBA 或通过使用 VBScript 的 COM 接口来做到这一点?

采纳答案by Ekkehard.Horner

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

根据这个 OLEDB init 属性表,您应该在连接字符串中添加“Mode=Share Exclusive”。

回答by HansUp

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:

这是 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