Recordset2.LockEdits プロパティ (DAO)

適用先: Access 2013、Office 2013

編集中に有効なロックの種類を示す値を設定または取得します。

構文

式 。LockEdits

式Recordset2 オブジェクトを表す変数。

注釈

次の表は、この設定値または戻り値が表すロック状態の種類です。

値

説明

True

既定値。 排他的ロックが有効になります。 Edit メソッドを呼び出した直後に、編集中のレコードが含まれているページがロックされます。

False

編集に対して共有的ロックが有効になります。 Update メソッドが実行されるまで、レコードを含むページはロックされません。

LockEdits プロパティは、更新可能な Recordset オブジェクトで使用できます。

ページがロックされている場合、他のユーザーは同じページのレコードを編集できません。 LockEdits プロパティを True に設定した場合、 Edit メソッドを使用するときに別のユーザーが既にページをロックしていると、エラーが発生します。 他のユーザーは、ロックされているページからデータを読み取ることができます。

LockEdits プロパティを False に設定した後、別のユーザーがページをロックしているときに Update メソッドを使用すると、エラーが発生します。 別のユーザーがレコードに加えた変更を確認するには、引数として 0 を指定して Move メソッドを使用します。ただし、これを行うと、変更は失われます。

Microsoft Access データベース エンジンに接続された ODBC データ ソースを使用する場合は、 LockEdits プロパティを常に False に設定するか、または共有的ロックを有効にします。 Microsoft Access データベース エンジンは、外部のデータベース サーバーで使用されるロック機能に対する制御は行いません。

注:

OpenRecordset メソッドの引数 lockedits を事前に設定しておくと、初めて Recordset オブジェクトを開くときに LockEdits プロパティの値を設定できます。 引数 lockedits を dbPessimistic に設定すると LockEdits プロパティが True に設定され、lockedits をそれ以外の値に設定すると LockEdits プロパティが False に設定されます。

例

この例は、まず LockEdits プロパティを True に設定して排他的ロックを有効にする方法を示し、次に LockEdits プロパティを False に設定して共有的ロックを有効にする方法を示します。 また、マルチユーザー データベースの環境でフィールドを変更するために必要なエラー処理の種類を示します。 このプロシージャを実行するには、PessimisticLock 関数および OptimisticLock 関数が必要です。

    Sub LockEditsX() 
     
     Dim dbsNorthwind As Database 
     Dim rstCustomers As Recordset2 
     Dim strOldName As String 
     
     Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
     Set rstCustomers = _ 
     dbsNorthwind.OpenRecordset("Customers", _ 
     dbOpenDynaset) 
     
     With rstCustomers 
     ' Store original data. 
     strOldName = !CompanyName 
     
     If MsgBox("Pessimistic locking demonstration...", _ 
     vbOKCancel) = vbOK Then 
     
     ' Attempt to modify data with pessimistic locking 
     ' in effect. 
     If PessimisticLock(rstCustomers, !CompanyName, _ 
     "Acme Foods") Then 
     MsgBox "Record successfully edited." 
     
     ' Restore original data... 
     .Edit 
     !CompanyName = strOldName 
     .Update 
     End If 
     
     End If 
     
     If MsgBox("Optimistic locking demonstration...", _ 
     vbOKCancel) = vbOK Then 
     
     ' Attempt to modify data with optimistic locking 
     ' in effect. 
     If OptimisticLock(rstCustomers, !CompanyName, _ 
     "Acme Foods") Then 
     MsgBox "Record successfully edited." 
     
     ' Restore original data... 
     .Edit 
     !CompanyName = strOldName 
     .Update 
     End If 
     
     End If 
     
     .Close 
     End With 
     
     dbsNorthwind.Close 
     
    End Sub 
     
    Function PessimisticLock(rstTemp As Recordset2, _ 
     fldTemp As Field, strNew As String) As Boolean 
     
     dim ErrLoop as Error 
     
     PessimisticLock = True 
     
     With rstTemp 
     .LockEdits = True 
     
     ' When you set LockEdits to True, you trap for errors 
     ' when you call the Edit method. 
     On Error GoTo Err_Lock 
     .Edit 
     On Error GoTo 0 
     
     ' If the Edit is still in progress, then no errors 
     ' were triggered; you may modify the data. 
     If .EditMode = dbEditInProgress Then 
     fldTemp = strNew 
     .Update 
     .Bookmark = .LastModified 
     Else 
     ' Retrieve current record to see changes made by 
     ' other user. 
     .Move 0 
     End If 
     
     End With 
     
     Exit Function 
     
    Err_Lock: 
     
     If DBEngine.Errors.Count > 0 Then 
     ' Enumerate the Errors collection. 
     For Each errLoop In DBEngine.Errors 
     MsgBox "Error number: " & errLoop.Number & _ 
     vbCr & errLoop.Description 
     Next errLoop 
     PessimisticLock = False 
     End If 
     
     Resume Next 
     
    End Function 
     
    Function OptimisticLock(rstTemp As Recordset, _ 
     fldTemp As Field, strNew As String) As Boolean 
     
     dim ErrLoop as Error 
     
     OptimisticLock = True 
     
     With rstTemp 
     .LockEdits = False 
     .Edit 
     fldTemp = strNew 
     
     ' When you set LockEdits to False, you trap for errors 
     ' when you call the Update method. 
     On Error GoTo Err_Lock 
     .Update 
     On Error GoTo 0 
     
     ' If there is no Edit in progress, then no errors were 
     ' triggered; you may modify the data. 
     If .EditMode = dbEditNone Then 
     ' Move current record pointer to the most recently 
     ' modified record. 
     .Bookmark = .LastModified 
     Else 
     .CancelUpdate 
     ' Retrieve current record to see changes made by 
     ' other user. 
     .Move 0 
     End If 
     
     End With 
     
     Exit Function 
     
    Err_Lock: 
     
     If DBEngine.Errors.Count > 0 Then 
     ' Enumerate the Errors collection. 
     For Each errLoop In DBEngine.Errors 
     MsgBox "Error number: " & errLoop.Number & _ 
     vbCr & errLoop.Description 
     Next errLoop 
     OptimisticLock = False 
     End If 
     
     Resume Next 
     
    End Function