>
Setting/Value | Description |
True (-1) | (Default) Pessimistic locking is in effect. The 2K page containing the record you're editing is locked as soon as you use the Edit method. |
False (0) | Optimistic locking is in effect for editing. The 2K page containing the record is not locked until the Update method is executed. |
Sub ShowLockStatus() Dim dbsNorthwind As Database, rstCustomers As Recordset Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb") Set rstCustomers = dbsNorthwind.OpenRecordset("Customers", dbOpenDynaset) rstCustomers.LockEdits = True OnError Goto ErrorHandler rstCustomers.Edit ' Start editing. On Error GoTo 0 rstCustomers.CancelUpdate rstCustomers.Close dbsNorthwind.Close Exit Sub ErrorHandler: If Err = 3197 Then Debug.Print "data changed" Resume Next Else Debug.Print "some other error" Resume Next End If End SubExample (Microsoft Access) The following example opens a Dynaset-Type Recordset object and sets its LockEdits property to False (0). This enables optimistic locking so other users can change the database records at any time. Microsoft Access triggers a trappable error if the data changes before you use the Update method.
Sub ShowLockStatus() Dim dbs As Database, rst As Recordset ' Return Database variable that points to current database. Set dbs = CurrentDb Set rst = dbs.OpenRecordset("Customers", dbOpenDynaset) rst.LockEdits = False On Error Goto ErrorHandler ' Enable editing. rst.Edit . ' Change records. . . rst.Update ' Try to post changes. rst.Close
Exit Sub ErrorHandler: If Err.Number = 3197 Then MsgBox "Data changed by another user." Resume Next Else MsgBox "Some other error." End If End SubExample (Microsoft Excel) This example locks the Customer recordset in the NWINDEX.MDB database before updating the value in the CUSTMR_ID field of the first record with the value in cell A1 on Sheet1. Locking the recordset ensures that no other user modifies the record while it is being updated. To create the NWINDEX.MDB database, run the Microsoft Excel example for the CreateDatabase method.
Dim db As Database, rs As Recordset Sheets("Sheet1").Cells(1, 1).Value = "ACRIM" databasePath = Application.Path & "\NWINDEX.MDB" Set db = DBEngine.Workspaces(0).OpenDatabase(databasePath) Set rs = db.OpenRecordset("Customer") valueToAdd = Sheets("Sheet1").Cells(1, 1).Value rs.LockEdits = False rs.Edit rs.fields("CUSTMR_ID").Value = valueToAdd rs.Update MsgBox "The new value of CUSTMR_ID is " & rs.fields("CUSTMR_ID").Value rs.Close db.Close