|
帮助文档里写得还是比较详尽的……一般FindFirst会结合Bookmark来使用【关于这个属性我已经发过两篇帖子,讲得应该算是比较清楚了,这里不再细说】,用于定位记录集。
此外,需要注意的是,FindFirst是DAO的方法,对应的ADO则是Find方法。- Sub FindFirstX()
- Dim dbsNorthwind As Database
- Dim rstCustomers As Recordset
- Dim strCountry As String
- Dim varBookmark As Variant
- Dim strMessage As String
- Dim intCommand As Integer
- Set dbsNorthwind = OpenDatabase("Northwind.mdb")
- Set rstCustomers = dbsNorthwind.OpenRecordset( _
- "SELECT CompanyName, City, Country " & _
- "FROM Customers ORDER BY CompanyName", _
- dbOpenSnapshot)
- Do While True
- ' Get user input and build search string.
- strCountry = _
- Trim(InputBox("Enter country for search."))
- If strCountry = "" Then Exit Do
- strCountry = "Country = '" & strCountry & "'"
- With rstCustomers
- ' Populate recordset.
- .MoveLast
- ' Find first record satisfying search string. Exit
- ' loop if no such record exists.
- .FindFirst strCountry
- If .NoMatch Then
- MsgBox "No records found with " & _
- strCountry & "."
- Exit Do
- End If
- Do While True
- ' Store bookmark of current record.
- varBookmark = .Bookmark
- ' Get user choice of which method to use.
- strMessage = "Company: " & !CompanyName & _
- vbCr & "Location: " & !City & ", " & _
- !Country & vbCr & vbCr & _
- strCountry & vbCr & vbCr & _
- "[1 - FindFirst, 2 - FindLast, " & _
- vbCr & "3 - FindNext, " & _
- "4 - FindPrevious]"
- intCommand = Val(Left(InputBox(strMessage), 1))
- If intCommand < 1 Or intCommand > 4 Then Exit Do
- ' Use selected Find method. If the Find fails,
- ' return to the last current record.
- If FindAny(intCommand, rstCustomers, _
- strCountry) = False Then
- .Bookmark = varBookmark
- MsgBox "No match--returning to " & _
- "current record."
- End If
- Loop
- End With
- Exit Do
- Loop
- rstCustomers.Close
- dbsNorthwind.Close
- End Sub
复制代码 |
|