Opening the Database in Shared Mode

If you want to implement recordset locking, you must open your database in shared mode. This is the typical mode for opening databases in multiuser environments. When a database is opened in shared mode, multiple users can simultaneously access the database and Microsoft Jet handles conflicts between users.

Û To open a database in shared mode

  1. Declare a database variable using the Dim statement.

  2. Use the OpenDatabase method to open the database, specifying a value of False for the options argument.

The following code checks to see whether a database is open exclusively, using the IsDbOpenedExclusively function defined in Chapter 2, “Introducing Data Access Objects.” If this function returns false, then the following code opens the database in shared mode. In this example, strDbPath is the path to the database:

Function OpenDatabaseShared(strDbPath As String) As Database
	Dim dbs As Database
  
	' Check whether database is open exclusively.
	If IsDbOpenedExclusively(strDbPath) Then
		MsgBox "Cannot open database in shared mode. " & _
			"You or another user may have it open exclusively."
		' Return Nothing.
		Set OpenDatabaseShared = Nothing
	Else
		' Open database and return reference.
		Set dbs = OpenDatabase(strDbPath, False)
		Set OpenDatabaseShared = dbs
	End If
End Function