• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 2. Developing Multiuser Applicat... > Testing a Record for Locking Status

Testing a Record for Locking Status

Often you want to determine the locking status of a record before you attempt an operation with it. By utilizing pessimistic locking and trying to modify the record, you can determine whether someone has locked the current row. The code looks like Listing 2.14.

Listing 2.14. Determining Whether a Record Is Locked Before Editing It

Sub TestLocking()

    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim boolLocked As Boolean

    Set cnn = New ADODB.Connection
    cnn.ConnectionString = " Provider=sqloledb;" & _
        "Data Source=(local);Initial Catalog=pubs;uid=sa;pwd="
    cnn.Open

    Set rst = New ADODB.Recordset
    rst.ActiveConnection = cnn
    rst.CursorType = adOpenKeyset
    rst.LockType = adLockPessimistic 'Invoke Pessimistic Locking
    rst.CursorLocation = adUseServer
    rst.Open "Authors", Options:=adCmdTable

    boolLocked = IsItLocked(rst)
    MsgBox boolLocked

End Sub

Function IsItLocked(rstAny As ADODB.Recordset) As Boolean
    On Error GoTo IsItLocked_Err
    IsItLocked = False

    With rstAny
        .Update
    End With
    Exit Function

IsItLocked_Err:
    If Err = -2147467259 Then
        IsItLocked = True
        Exit Function
    End If
End Function


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint