我希望能够编写一个脚本,以独占模式打开Access数据库,这样我就可以刷新其中的信息,而不必担心其他用户在不一致的状态下访问数据库。是否可以使用VBA或通过使用VBScript的COM接口来实现这一点?
我不知道当你的脚本启动时,如果有用户正在使用数据库会发生什么。所以我选择检查数据库锁定文件的存在,并且只有在锁定文件不存在时才继续执行。
这里是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