ADO记录集对象的支持方法

创建ADO Recordset时,您应该对Recordset不提供/不提供什么功能有所了解。 一些关键问题可能并且应该是:

  1. 我可以将新记录添加到记录集吗?
  2. 记录集是否支持书签?
  3. 我们可以在此记录集中使用查找和/或查找方法吗?
  4. 记录集是否支持索引的使用?
  5. 是否可以在此Recordset上使用Absoluteposition属性?
  6. 等等....

幸运的是,通过使用ADO Recordset对象的Supports方法,可以轻松地回答所有这些问题以及更多问题。 此方法确定ADO Recordset对象支持哪种功能。 它返回一个布尔值(真/假),该值指示提供者是否支持其单个参数所标识的所有功能。 如果支持特定功能,则返回True,否则返回False。

为了演示此方法的使用,我将:
  1. 显示一个名为RecordsetSupport()的“包装程序”功能子例程,该例程接受一个参数(一个ADO Recordset对象)。
  2. 创建2个非常不同的ADO记录集。
  3. 将Recordset对象传递给RecordsetSupport()子例程。
  4. 显示每个记录集的输出,指示每个记录集支持的功能。
  5. 依次对每个Recordset执行前面的步骤。
Public Sub RecordsetSupport(rst As ADODB.Recordset)
If rst.Supports(adAddNew) Then
  Debug.Print "Supports AddNew"
Else
  Debug.Print "Doesn't support AddNew"
End If 
If rst.Supports(adApproxPosition) Then
  Debug.Print "Supports AbsolutePosition"
Else
  Debug.Print "Doesn't support AbsolutePosition"
End If 
If rst.Supports(adBookmark) Then
  Debug.Print "Supports Bookmarks"
Else
  Debug.Print "Doesn't support Bookmarks"
End If 
If rst.Supports(adDelete) Then
  Debug.Print "Supports Delete"
Else
  Debug.Print "Doesn't support Delete"
End If 
If rst.Supports(adFind) Then
  Debug.Print "Supports Find"
Else
  Debug.Print "Doesn't support Find"
End If 
If rst.Supports(adHoldRecords) Then
  Debug.Print "Supports Move without Save"
Else
  Debug.Print "Doesn't support Move without Save"
End If 
If rst.Supports(adIndex) Then
  Debug.Print "Supports Index"
Else
  Debug.Print "Doesn't support Index"
End If 
If rst.Supports(adMovePrevious) Then
  Debug.Print "Supports MovePrevious"
Else
  Debug.Print "Doesn't support MovePrevious"
End If 
If rst.Supports(adResync) Then
  Debug.Print "Supports Resync"
Else
  Debug.Print "Doesn't support Resync"
End If 
If rst.Supports(adSeek) Then
  Debug.Print "Supports Seek"
Else
  Debug.Print "Doesn't support Seek"
End If 
If rst.Supports(adUpdate) Then
  Debug.Print "Supports Update"
Else
  Debug.Print "Doesn't support Update"
End If 
If rst.Supports(adUpdateBatch) Then
  Debug.Print "Supports UpdateBatch"
Else
  Debug.Print "Doesn't support UpdateBatch"
End If
End Sub
'Recordset #1 (Restrictive)
Dim rstSupports1 As ADODB.Recordset, strSQL As String 
Set rstSupports1 = New ADODB.Recordset 
strSQL = "Select * From Employees;" 
With rstSupports1
  .Source = strSQL
  .ActiveConnection = CurrentProject.Connection
  .CursorType = adOpenForwardOnly
  .LockType = adLockReadOnly
End With 
rstSupports1.Open , , , , adCmdText 
Debug.Print "-----------------------------------------------------------------------------------"
Debug.Print "SQL Statement Source | Forward Only Cursor | Read Only Recordset | acCmdText Option"
Debug.Print "-----------------------------------------------------------------------------------" 
Call RecordsetSupport(rstSupports1) 
rstSupports1.Close
Set rstSupports1 = Nothing
'Recordset #2 (Open)
On Error GoTo Err_Command115_Click 
Dim rstSupports2 As ADODB.Recordset 
Set rstSupports2 = New ADODB.Recordset 
With rstSupports2
  .Source = "Employees"
  .ActiveConnection = CurrentProject.Connection
  .CursorType = adOpenKeyset
  .LockType = adLockOptimistic
End With 
rstSupports2.Open , , , , adCmdTableDirect 
Debug.Print "-----------------------------------------------------------------------------------"
Debug.Print "Table Type Recordset | Keyset Cursor | Optimistic Locking | acCmdTableDirect Option"
Debug.Print "-----------------------------------------------------------------------------------" 
Call RecordsetSupport(rstSupports2) 
rstSupports2.Close
Set rstSupports2 = Nothing
记录集1的输出
-----------------------------------------------------------------------------------
SQL Statement Source | Forward Only Cursor | Read Only Recordset | acCmdText Option
-----------------------------------------------------------------------------------
Doesn't support AddNew
Doesn't support AbsolutePosition
Doesn't support Bookmarks
Doesn't support Delete
Supports Find
Doesn't support Move without Save
Doesn't support Index
Doesn't support MovePrevious
Supports Resync
Doesn't support Seek
Doesn't support Update
Doesn't support UpdateBatch
记录集2的输出
-----------------------------------------------------------------------------------
Table Type Recordset | Keyset Cursor | Optimistic Locking | acCmdTableDirect Option
-----------------------------------------------------------------------------------
Supports AddNew
Doesn't support AbsolutePosition
Supports Bookmarks
Supports Delete
Supports Find
Supports Move without Save
Supports Index
Supports MovePrevious
Doesn't support Resync
Supports Seek
Supports Update
Supports UpdateBatch

From: https://bytes.com/topic/access/insights/723038-supports-method-ado-recordset-object

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值