The first user to open an Access database determines which mode is used while the database is open. Thus, if the first user uses row-level locking to open the database, users that subsequently open the database will use row-level locking.
When you use the Microsoft OLE DB Provider for Jet 4.0, you can configure row-level locking from within ADO. To enable row-level locking by using ADO, select the Jet 4 Provider, and then set the
Connection object's dynamic
Jet OLEDB:Database Locking Mode property to 1.
To enforce DAO to use the row-level locking that ADO sets, follow these steps:
- Use row-level locking to open an ADO Connection to the database as follows:
- Set the ADO Connection object's Provider property to
Microsoft.JET.OLEDB.4.0.
- Set the Connection object's dynamic Properties("Jet OLEDB:Database Locking Mode") to 1.
- Open the ADO Connection.
- Use the OpenDatabase method to open the same database from DAO.
Because the locking mode is reset when you close and reopen the database, use a DAO database that remains open as long as you need row-level locking. For example, use Form or Module level scope in Visual Basic for the DAO database. - Close the ADO Connection.
Sample Code
The code sample to follow demonstrates the sequence of statements to configure row-level locking by using ADO and then opens a DAO connection to the database by using row-level locking. In this sample, the DAO database is declared at Form level so that all DAO databases that are opened within the Form use row-level locking. In addition, this sample uses the Access 2000 Northwind.mdb sample database, which is copied to a folder named C:\MDB.
- In Visual Basic, create a new Standard EXE project. Form1 is created by default.
- From the Project menu, click References, and then select the following check boxes:
Microsoft ActiveX Data Objects Library
Microsoft DAO 3.6 Object Library
- Paste the following code into Form1's Code window:
'This sample uses Form level scope for the open DAO database.
'Depending on your application, you may choose Module level.
Private wsDAO As DAO.Workspace
Private dbDAO As DAO.Database
Private Sub Form_Load()
Dim cnn As ADODB.Connection
'ADO has the ability to open row-level locking; DAO does not.
'The following code is used to implement row-level locking in DAO.
'If the database is opened first in row-level locking in ADO,
'subsequent attempts to open the database in ADO and DAO will use the same mode.
Set cnn = New ADODB.Connection
cnn.Provider = "Microsoft.JET.OLEDB.4.0"
cnn.Properties("Data Source") = "C:\MDB\Northwind.mdb"
cnn.Properties("Jet OLEDB:Database Locking Mode") = 1
cnn.CursorLocation = adUseServer
cnn.Open
Set wsDAO = DBEngine.CreateWorkspace("WorkSpace", "Admin", "", dbUseJet)
Set dbDAO = wsDAO.OpenDatabase("C:\MDB\Northwind.mdb")
'Close the ADODB connection.
cnn.Close
Set cnn = Nothing
End Sub