Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Access database in exclusive mode

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?

like image 653
Tmdean Avatar asked Oct 18 '25 09:10

Tmdean


1 Answers

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
like image 81
HansUp Avatar answered Oct 20 '25 10:10

HansUp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!