CursorType、LockType 和 EditMode 属性范例

该范例说明如何在打开 Recordset 之前设置 CursorTypeLockType 属性。同时还显示不同情况下 EditMode 的属性值。该过程运行时需要 EditModeOutput 函数。

Public Sub EditModeX()   Dim cnn1 As ADODB.Connection
   Dim rstEmployees As ADODB.Recordset
   Dim strCnn As String   ' 使用雇员表中的数据打开记录集。
   Set cnn1 = New ADODB.Connection
   strCnn = "Provider=sqloledb;" & _
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
   cnn1.Open strCnn
      
   Set rstEmployees = New ADODB.Recordset
   Set rstEmployees.ActiveConnection = cnn1
   rstEmployees.CursorType = adOpenKeyset
   rstEmployees.LockType = adLockBatchOptimistic
   rstEmployees.Open "employee", , , , adCmdTable   ' 显示不同编辑状态下的 EditMode 属性。
   rstEmployees.AddNew
   rstEmployees!emp_id = "T-T55555M"
   rstEmployees!fname = "temp_fname"
   rstEmployees!lname = "temp_lname"
   EditModeOutput "After AddNew:", rstEmployees.EditMode
   rstEmployees.UpdateBatch
   EditModeOutput "After UpdateBatch:", rstEmployees.EditMode
   rstEmployees!fname = "test"
   EditModeOutput "After Edit:", rstEmployees.EditMode
   rstEmployees.Close
   
   ' 删除新记录,因为这只是演示。
   cnn1.Execute "DELETE FROM employee WHERE emp_id = 'T-T55555M'"End SubPublic Function EditModeOutput(strTemp As String, _
   intEditMode As Integer)   ' 打印基于 EditMode 属性值的报表。
   Debug.Print strTemp
   Debug.Print "  EditMode = ";   Select Case intEditMode
      Case adEditNone
         Debug.Print "adEditNone"
      Case adEditInProgress
         Debug.Print "adEditInProgress"
      Case adEditAdd
         Debug.Print "adEditAdd"
   End SelectEnd Function
www.2mysite.net