在Recordset对象里面查询记录地办法

2016-02-19 17:28 3 1 收藏

最近很多朋友喜欢上设计,但是大家却不知道如何去做,别担心有图老师给你解答,史上最全最棒的详细解说让你一看就懂。

【 tulaoshi.com - 编程语言 】

  无论是 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", _

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

' "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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

' 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

例如,用用这个过程查询“罗期文商贸”示例数据库中“客户”表的“国家”等于 USA 的记录,可以使用下面的代码:

FindRecord “c:Program FilesMicrosoft OfficeOfficeSamplesNorthwind.mdb”, _

“Customers”, “Country=' USA '”, ”CustomerID”

( 译者注:如果你安装的是简体中文版要将相应的字段名改成中文)

用 ADO Seek 方法

因为 ADO Seek 方法使用 Index ,最好是在用这个方法之前指定一个索引,可是,如果你没有指定索引, Jet database engine 将用主键。

如果你需要从多个字段中指定值做为搜索条件,可以用 VBA 中的 Array 函数传递这些值到参数 KeyValues 中去。如果你只需要从一个字段中指定值做为搜索条件,则不需要用 Array 函数传递。

象 Find 方法一样,你可以用 BOF 或者 EOF 属性测试是否查询到记录。

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

Sub SeekRecord(strDBPath As String, _

strIndex As String, _

strTable As String, _

varKeyValues As Variant, _

strDisplayField As String)

' This procedure finds a record by using

' the specified index and key values.

' For example, to use the PrimaryKey index to

' find records in the Order Details table in the

' Northwind database where the OrderID field is

' 10255 and ProductID is 16, and then display the

' value in the Quantity field, you can use a line

' of code like this:

' SeekRecord _

' "c:Program FilesMicrosoft OfficeOfficeSamplesNorthwind.mdb", _

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

' "PrimaryKey", "Order Details", Array(10255, 16), "Quantity"

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

' Select the index used to order the

' data in the recordset.

.Index = strIndex

' Open the table by using a scrolling

' Recordset object.

.Open Source:=strTable, _

ActiveConnection:=cnn, _

CursorType:=adOpenKeyset, _

LockType:=adLockOptimistic, _

Options:=adCmdTableDirect

' Find the order where OrderId = 10255 and

' ProductId = 16.

.Seek KeyValues:=varKeyValues, SeekOption:=adSeekFirstEQ

' If a match is found, print the value of

' the specified field.

If Not .EOF Then

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

Debug.Print .Fields(strDisplayField).Value

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

例如,用主键索引查询“罗期文商贸”示例数据库中“订单明细”表的“订单编号”等于 10255 并且产品编号等于 16 的 ” 数量 ” 的值,可以使用下面的代码:

SeekRecord “c:Program FilesMicrosoft OfficeOfficeSamplesNorthwind.mdb”, _

“PrimaryKey”, “Order Details”, Array(10255,16), ”Quantity”

( 译者注:如果你安装的是简体中文版要将示例中引用的相应的字段名改成中文 )

来源:https://www.tulaoshi.com/n/20160219/1615302.html

延伸阅读
标签: Web开发
 假设这个数据表有一个唯一的ID字段,并至少有一条记录。随机存取其中一条记录的方法是非常简单的,可以分为四步: 1、取得记录总数n。 2、把所有的ID号存储到一个数组中 3、产生一个不大于n的随机数m 4、从数组中取出第m个ID号,查询数据表,取得记录数据。 下面是部分代码: $#@60;% set conn = Server.CreateObject(‘ADODB...
一,子选择基本用法 1,子选择的定义 子迭择允许把一个查询嵌套在另一个查询当中。比如说:一个考试记分项目把考试事件分为考试(T)和测验(Q)两种情形。下面这个查询就能只找出学生们的考试成绩 select * from score where event_id in (select event_id from event where t...
花钱大师在哪查询记录?   花钱大师方便记账工具,可以统计分析你的日常消费状况,记录的每一笔消费都可以通过查询翻阅记录,接下来小编就教大家花钱大师怎么查询记录? 1)打开进入首页后点击功能。接下来选择你需要查看的记事本。 2)点击进入后点击右上角图标。选择你要查看的分类时间等点击即可。 以上...
标签: 电脑入门
如何查询QQ登录记录,当然如果您是QQ会员那相当方便,打开QQ,点击下方的消息管理器,在系统消息中就可以看到上次登录的时间和IP地址;那么如果不是QQ会员怎么查QQ登陆记录呢?请接着往下看; 非QQ会员怎么查QQ登录记录: 第一步:打开QQ后,点击面板上的QQ邮箱图标,打开QQ邮箱; 第二步:打开QQ邮箱后,在欢迎页面的正文中间,广告图片下方找...
在使用 mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。其原因是distinct只能返回它的目标字段,而无法返回其它字段,这个问题让我困扰了很久,用distinct不能解决的话,我只有用二重循环查询...

经验教程

740

收藏

52
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部