设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 3471|回复: 6
打印 上一主题 下一主题

[ADO/DAO] 怎样判断recordset.findfirst方法是否找到了记录?

[复制链接]
跳转到指定楼层
1#
发表于 2013-4-27 11:22:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
能否判断recordset.findfirst方法是否找到了记录?还是先判断记录是否存在,如果是再用这个方法定位?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2013-4-27 13:13:38 | 只看该作者
先找,找不到再判断不存在.
3#
 楼主| 发表于 2013-4-27 14:14:14 | 只看该作者
roych 发表于 2013-4-27 13:13
先找,找不到再判断不存在.

如何判断找不到?
if me.序号.Value<>me.txtSearch.Value then......?

点击这里给我发消息

4#
发表于 2013-4-27 15:56:13 | 只看该作者
rst.NoMatch=false 就是找到了,否则找不到
5#
发表于 2013-4-27 23:58:36 | 只看该作者
帮助文档里写得还是比较详尽的……一般FindFirst会结合Bookmark来使用【关于这个属性我已经发过两篇帖子,讲得应该算是比较清楚了,这里不再细说】,用于定位记录集。
此外,需要注意的是,FindFirst是DAO的方法,对应的ADO则是Find方法。
  1. Sub FindFirstX()

  2.    Dim dbsNorthwind As Database
  3.    Dim rstCustomers As Recordset
  4.    Dim strCountry As String
  5.    Dim varBookmark As Variant
  6.    Dim strMessage As String
  7.    Dim intCommand As Integer

  8.    Set dbsNorthwind = OpenDatabase("Northwind.mdb")
  9.    Set rstCustomers = dbsNorthwind.OpenRecordset( _
  10.       "SELECT CompanyName, City, Country " & _
  11.       "FROM Customers ORDER BY CompanyName", _
  12.       dbOpenSnapshot)

  13.    Do While True
  14.       ' Get user input and build search string.
  15.       strCountry = _
  16.          Trim(InputBox("Enter country for search."))
  17.       If strCountry = "" Then Exit Do
  18.       strCountry = "Country = '" & strCountry & "'"

  19.       With rstCustomers
  20.          ' Populate recordset.
  21.          .MoveLast
  22.          ' Find first record satisfying search string. Exit
  23.          ' loop if no such record exists.
  24.          .FindFirst strCountry
  25.          If .NoMatch Then
  26.             MsgBox "No records found with " & _
  27.                strCountry & "."
  28.             Exit Do
  29.          End If

  30.          Do While True
  31.             ' Store bookmark of current record.
  32.             varBookmark = .Bookmark
  33.             ' Get user choice of which method to use.
  34.             strMessage = "Company: " & !CompanyName & _
  35.                vbCr & "Location: " & !City & ", " & _
  36.                !Country & vbCr & vbCr & _
  37.                strCountry & vbCr & vbCr & _
  38.                "[1 - FindFirst, 2 - FindLast, " & _
  39.                vbCr & "3 - FindNext, " & _
  40.                "4 - FindPrevious]"
  41.             intCommand = Val(Left(InputBox(strMessage), 1))
  42.             If intCommand < 1 Or intCommand > 4 Then Exit Do

  43.             ' Use selected Find method. If the Find fails,
  44.             ' return to the last current record.
  45.             If FindAny(intCommand, rstCustomers, _
  46.                   strCountry) = False Then
  47.                .Bookmark = varBookmark
  48.                MsgBox "No match--returning to " & _
  49.                   "current record."
  50.             End If

  51.          Loop

  52.       End With

  53.       Exit Do
  54.    Loop

  55.    rstCustomers.Close
  56.    dbsNorthwind.Close

  57. End Sub
复制代码
6#
 楼主| 发表于 2013-4-28 22:34:55 | 只看该作者
本帖最后由 网络蜘蛛 于 2013-4-28 22:36 编辑
t小宝 发表于 2013-4-27 15:56
rst.NoMatch=false 就是找到了,否则找不到

成功了,多谢版主。
7#
 楼主| 发表于 2013-4-28 22:38:18 | 只看该作者
roych 发表于 2013-4-27 23:58
帮助文档里写得还是比较详尽的……一般FindFirst会结合Bookmark来使用【关于这个属性我已经发过两篇帖子,讲 ...

多谢版主深夜还在回复我的问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-4 12:17 , Processed in 0.150418 second(s), 30 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表