Office中国论坛/Access中国论坛

标题: 怎样判断recordset.findfirst方法是否找到了记录? [打印本页]

作者: 网络蜘蛛    时间: 2013-4-27 11:22
标题: 怎样判断recordset.findfirst方法是否找到了记录?
能否判断recordset.findfirst方法是否找到了记录?还是先判断记录是否存在,如果是再用这个方法定位?

作者: roych    时间: 2013-4-27 13:13
先找,找不到再判断不存在.
作者: 网络蜘蛛    时间: 2013-4-27 14:14
roych 发表于 2013-4-27 13:13
先找,找不到再判断不存在.

如何判断找不到?
if me.序号.Value<>me.txtSearch.Value then......?
作者: t小宝    时间: 2013-4-27 15:56
rst.NoMatch=false 就是找到了,否则找不到
作者: roych    时间: 2013-4-27 23:58
帮助文档里写得还是比较详尽的……一般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
复制代码

作者: 网络蜘蛛    时间: 2013-4-28 22:34
本帖最后由 网络蜘蛛 于 2013-4-28 22:36 编辑
t小宝 发表于 2013-4-27 15:56
rst.NoMatch=false 就是找到了,否则找不到

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

多谢版主深夜还在回复我的问题。




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3