NoLongerSet
Member
- Local time
- Yesterday, 21:53
- Joined
- Jul 13, 2021
- Messages
- 31
If you use the
I wrote a function that acts as a partial workaround for this issue. Namely, it helps re-enable single-user access to backend databases when using the
The full article is here: Partial Workaround for the "Could not lock file" Bug
Here's the raw code:
DBEngine.OpenDatabase
method and started getting the following error message in your Access applications, you are likely a victim of a recent Microsoft security fix that had some nasty side effects for us Access developers.I wrote a function that acts as a partial workaround for this issue. Namely, it helps re-enable single-user access to backend databases when using the
OpenDatabase
method, but it still does not solve the (bigger) multi-user issue. However, it can still be valuable in some scenarios (such as during development).The full article is here: Partial Workaround for the "Could not lock file" Bug
Here's the raw code:
Code:
'This is a temporary workaround for the December 2021 Access bug:
' https://nolongerset.com/bug-alert-file-in-use-could-not-lock-file/
'Note that this fix works only for *single-user* access to the Access file,
' as it falls back on opening the file for exclusive use
'
'Source: https://nolongerset.com/could-not-lock-file-workaround/
'
'USAGE:
' REPLACE: Set MyDb = DBEngine.OpenDatabase(PathToMyDb)
' WITH: Set MyDb = OpenDatabase(DBEngine, PathToMyDb)
Function OpenDatabase(Engine As DAO.DBEngine, PathToDB As String, _
Optional Options As Variant = False, _
Optional ReadOnly As Variant = False, _
Optional Connect As Variant) As DAO.Database
On Error Resume Next
Set OpenDatabase = Engine.OpenDatabase(PathToDB, Options, ReadOnly, Connect)
Select Case Err.Number
Case 0 'no error; safe to return
Exit Function
Case 3050 'Could not lock file.
'Attempt to open file with exclusive access
Err.Clear
Set OpenDatabase = Engine.OpenDatabase(PathToDB, True, ReadOnly, Connect)
If Err.Number = 0 Then
'If it succeeds, notify the developer then exit the function
Debug.Print PathToDB; " opened with exclusive access only"
Exit Function
End If
End Select
'If we got here it means that the workaround did not fix the problem;
' (maybe another user has the file open and we can't get exclusive access)
' we'll nullify the On Error Resume Next handler and re-run the method
' so that we properly propagate the original error up the call stack
On Error GoTo 0
Set OpenDatabase = Engine.OpenDatabase(PathToDB, Options, ReadOnly, Connect)
End Function