在Recordset对象中查询记录的方法

来源:ddvip.com 时间:2008-11-20 16:51:00 

无论是 DAO 还是 ADO 都有两种从 Recordset 对象中查询记录的方法: Find 方法和 Seek 方法。在这两种方法中可以让你指定条件进行查询与其相应的记录 , 一般而言,在相同条件下, Seek 方法提供了比 Find 方法更好的性能,因为 Seek 方法是基于索引的。因为这个原因基本提供者必须支持 Recordset 对象上的索引,可以用 Supports ( adSeek ) 方法确定基本提供者是否支持 Seek ,用 Supports ( adIndex ) 方法确定提供者是否支持索引。(例如, OLE DB Provider for Microsoft Jet 支持 Seek 和 Index 。),请将 Seek 方法和 Index 属性结合使用。如果 Seek 没有找到所需的行,将不会产生错误,该行将被放在 Recordset 的结尾处。执行此方法前,请先将 Index 属性设置为所需的索引。此方法只受服务器端游标支持。如果 Recordset 对象的 CursorLocation 属性值为 adUseClient ,将不支持 Seek 。只有当 CommandTypeEnum 值为 adCmdTableDirect 时打开 Recordset 对象,才可以使用此方法。

用 ADO Find 方法

DAO 包含了四个“ Find ”方法: FindFirst,FindLast,FindNext 和 FindPrevious .

DAO 方法 ADO Find 方法

下面的一个例子示范了如何用 ADO Find 方法查询记录:



Sub FindRecord(strDBPath As String, _
strTable As String, _
strCriteria As String, _
strDisplayField As String)
' This procedure finds a record in the specified table by
' using the specified criteria.
' For example, to use this procedure to find records
' in the Customers table in the Northwind database
' that have " USA " in the Country field, you can
' use a line of code like this:
' FindRecord _
' "c:Program FilesMicrosoft OfficeOfficeSamplesNorthwind.mdb", _
' "Customers", "Country=' USA '", "CustomerID"
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
' Open the Connection object.
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open strDBPath
End With
Set rst = New ADODB.Recordset
With rst
' Open the table by using a scrolling
' Recordset object.
.Open Source:=strTable, _
ActiveConnection:=cnn, _
CursorType:=adOpenKeyset, _
LockType:=adLockOptimistic
' Find the first record that meets the criteria.
.Find Criteria:=strCriteria, SearchDirection:=adSearchForward
' Make sure record was found (not at end of file).
If Not .EOF Then
' Print the first record and all remaining
' records that meet the criteria.
Do While Not .EOF
Debug.Print .Fields(strDisplayField).Value
' Skip the current record and find next match.
.Find Criteria:=strCriteria, SkipRecords:=1
Loop
Else
MsgBox "Record not found"
End If
' Close the Recordset object.
.Close
End With
' Close connection and destroy object variables.
cnn.Close
Set rst = Nothing
Set cnn = Nothing
End Sub

标签:
0
投稿

猜你喜欢

  • Python基于回溯法子集树模板实现图的遍历功能示例

    2021-10-29 15:20:31
  • Sql Server 和 Access 操作数据库结构Sql语句

    2008-02-11 18:59:00
  • python基于itchat模块实现微信防撤回

    2022-03-15 01:41:42
  • js DNA动态序列比对代码

    2024-04-16 10:41:26
  • sql server 2008 忘记sa密码的解决方法

    2024-01-26 22:48:16
  • JS与Ajax Get和Post在使用上的区别实例详解

    2024-04-23 09:07:40
  • 浅谈django的render函数的参数问题

    2022-07-10 18:39:20
  • Python HTTP客户端自定义Cookie实现实例

    2023-12-16 01:19:30
  • 给你一面国旗 教你用python画中国国旗

    2023-06-25 15:10:36
  • MySQL字段类型说明

    2007-09-27 19:22:00
  • Python使用Srapy框架爬虫模拟登陆并抓取知乎内容

    2022-02-02 11:08:01
  • Python 遍历子文件和所有子文件夹的代码实例

    2021-02-09 17:21:34
  • python绘制漏斗图步骤详解

    2021-06-17 22:57:57
  • np.where()[0] 和 np.where()[1]的具体使用

    2023-04-21 20:21:29
  • 使用python编写android截屏脚本双击运行即可

    2021-01-25 20:47:19
  • Python虚拟机字节码教程之控制流实现详解

    2023-01-09 09:56:53
  • python re.match()用法相关示例

    2023-07-21 05:12:30
  • python 剪切移动文件的实现代码

    2023-06-10 05:21:14
  • pytest用yaml文件编写测试用例流程详解

    2022-04-19 03:55:29
  • 浅谈ADO.NET数据库脚本

    2024-01-22 16:51:23
  • asp之家 网络编程 m.aspxhome.com