Determining Whether the Current Database Is Open Exclusively

Determining whether the current database is open exclusively isn’t simply a matter of trying to open the database. If you have the database open exclusively and try to open it again, you won’t receive a run-time error as you may expect. Actually, you can open a database as many times as you want to, exclusively or not, with the same DBEngine object.

To determine whether the current database is open exclusively, you can try to open the database by using the PrivDBEngine object, as shown in the following code:

Function IsDbOpenedExclusively(strDbPath As String) As Boolean
	On Error Resume Next
	Dim dbe As PrivDBEngine
	Dim wrk As Workspace
	Dim dbs As Database
	Const conFileInUse = 3045
	Const conDBOpenedExclusively = 3356

	' Return reference to private DBEngine object.
	Set dbe = New PrivDBEngine
	' Return reference to default workspace.
	Set wrk = dbe.Workspaces(0)
	' Attempt to open database.
	Set dbs = wrk.OpenDatabase(strDbPath)
	' If reference to database isn't returned, check error.
	If dbs Is Nothing Then
		' If error indicates database is open exclusively, return True.
		If (Err = conFileInUse Or Err = conDBOpenedExclusively) Then
			IsDbOpenedExclusively = True
		' If unanticipated error occurs, display message.
		Else
			MsgBox "Error: " & Err & ": " & vbCrLf & Err.Description
		End If
	' If reference to database is returned, it must not be opened
	' exclusively by any other user.
	Else
		IsDbOpenedExclusively = False
		dbs.Close
		Set dbs = Nothing
	End If
End Function

If this function generates error 3045 or error 3356, then it returns True because these errors indicate that the database is already opened exclusively.