Record locking occurs in two environments, the Microsoft Access Interface and the ADO Recordset Object. We have already established that record locking is in effect mostly in network shares. But what is record locking exactly? Well it is a mechanism that locks records when they are being edited by a particular user so that other users cannot edit them.
To set the record locking options in the Interface simply go to the Advanced Tab in the Options dialog box and set the options under the Default Record Locking group box:
As you can see from the image above there are three types of record locking options:
Like many of the other settings on the Advanced tab of the Options dialog box, the Default Record Locking setting applies to the Access application as a whole, and not to the individual database application that is opened.
When a user tries to edit a locked record the following message is displayed:
A user can either save the changes (even if the particular record has been changed by another user), copy them to the Windows clipboard or abandon the changes.
When setting the LockType property of the ADO Recordset, you use one of the following values:
Once we’ve decided on what type of locking we want to use in our code, we simply specify it as a parameter to the Open method of the recordset object or assign it to the recordset object’s LockType property before opening the recordset. Take a look at the following example code which shows how to use the LockType property as explained:
Dim dbcon As ADODB.Connection Dim recrdst As ADODB.Recordset Set recrdst = New ADODB.Recordset recrdst.CursorType = adOpenKeyset recrdst.LockType = adLockOptimistic Set dbcon = New ADODB.Connection dbcon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\generic.mdb;" recrdst.Open "SELECT * FROM names", dbcon Set Me.Recordset = recrdst recrdst.Close dbcon.Close Set recrdst = Nothing Set con = Nothing End Sub
In the code above, the LockType is set before the recordset is opened. The code below shows how to specify the LockType as a parameter to the open method:
Dim dbcon As ADODB.Connection Dim recrdst As ADODB.Recordset Set recrdst = New ADODB.Recordset recrdst.CursorType = adOpenKeyset recrdst.LockType = adLockOptimistic Set dbcon = New ADODB.Connection dbcon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\generic.mdb;" recrdst.Open "SELECT * FROM names", dbcon ,adOpenKeyset, adLockOptimistic Set Me.Recordset = recrdst recrdst.Close dbcon.Close Set recrdst = Nothing Set con = Nothing End Sub