当前位置: 首页 > 图文教程 > 数据库 > Access > 在Recordset对象中查询记录的方法

Access
中文Access2000速成教程--1.2 使用“数据库向导”创建表
建立自由的会计日期的报表--1.2.创建让用户选择日期窗体
建立自由的会计日期的报表--1.1.认识几个有关时间的函数
建立自由的会计日期的报表
使用准则进行条件查询--1.5.常用的准则表达式
使用准则进行条件查询--1.4.从窗体中选择查询的条件
中文Access2000速成教程--1.1 使用“向导”设计数据库
获取ACCESS2000数据库中所有表的名称
中文Access2000速成教程--1.3 在“设计”视图中设计表
中文Access2000速成教程--1.4 使用“表向导”建立新表
中文Access2000速成教程--1.8 定义表之间的关系
中文Access2000速成教程--1.5 使用已有的数据自动建新表
中文Access2000速成教程--1.6 定义“主键”
中文Access2000速成教程--1.7 创建索引
解决Access中分组报表的问题
用 INNER JOIN语法联接多个表建记录集
ACCESS学习日记
Access保留字&变量名列表
如何 在Access中选择指定日期前的记录?
ACCESS中关于SQL语句的转义字符

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 31 ::
收藏到网摘: n/a

无论是 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