创建ADO Recordset时,您应该对Recordset不提供/不提供什么功能有所了解。 一些关键问题可能并且应该是:
- 我可以将新记录添加到记录集吗?
- 记录集是否支持书签?
- 我们可以在此记录集中使用查找和/或查找方法吗?
- 记录集是否支持索引的使用?
- 是否可以在此Recordset上使用Absoluteposition属性?
- 等等....
幸运的是,通过使用ADO Recordset对象的Supports方法,可以轻松地回答所有这些问题以及更多问题。 此方法确定ADO Recordset对象支持哪种功能。 它返回一个布尔值(真/假),该值指示提供者是否支持其单个参数所标识的所有功能。 如果支持特定功能,则返回True,否则返回False。
为了演示此方法的使用,我将:- 显示一个名为RecordsetSupport()的“包装程序”功能子例程,该例程接受一个参数(一个ADO Recordset对象)。
- 创建2个非常不同的ADO记录集。
- 将Recordset对象传递给RecordsetSupport()子例程。
- 显示每个记录集的输出,指示每个记录集支持的功能。
- 依次对每个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