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 accessin开发者_如何学编程g the database in an inconsistent state. Is there a way to do this using VBA, or through a COM interface using VBScript?
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
According to this table of OLEDB init properties, you should add a "Mode=Share Exclusive" to your connection string.
精彩评论