Comparison of Seek Versus Find Methods, for VB Data AccessID: Q108149
|
This article compares the Seek method to the find methods (FindFirst, FindLast, FindNext, and FindPrevious) for data access in Visual Basic.
The Seek and find methods differ in performance and in the type of
recordsets to which they apply:
<recordset>.FindFirst <criteria>
where <recordset> and <criteria> are defined as follows:
<recordset> is the Recordset property of a data control or an object
variable identifying a Dynaset or Snapshot.
<criteria> is a string expression specifying the records that you
want. The string is the WHERE clause in an SQL string without the
word WHERE.
Sub Form_Load ()
Dim MyCriteria As String, MyDB As Database, MySet As Dynaset
MyCriteria = "State = 'NY'" ' Define search criteria.
Set MyDB = OpenDatabase("BIBLIO.MDB")
' Create a Dynaset based on the Publishers table:
Set MySet = MyDB.CreateDynaset("Publishers")
' Find first matching record:
MySet.FindFirst MyCriteria
If Not MySet.NoMatch Then
MsgBox "match was found"
Else
MsgBox "match was not found"
End If ' For a data control, you can use Data1.Recordset.NoMatch
' Find next matching record:
MySet.FindNext MyCriteria
If Not MySet.NoMatch Then
MsgBox "match was found"
Else
MsgBox "match was not found"
End If
End Sub
<table>.Seek <comparison>, <key1>, <key2>, ...
where the arguments are defined as follows:
<comparison> is one of the following string expressions:
<, <=, =, >=, >, or <>
<key1>, <key2>, ... one value for each field in the table's current
index.
Sub Form_Load ()
Dim MyDB As database, MyTable As table
Set MyDB = OpenDatabase("BIBLIO.MDB") ' Open a database.
Set MyTable = MyDB.OpenTable("Publishers") ' Open a table.
MyTable.Index = "PrimaryKey" ' Define current index.
MyTable.Seek "=", 3 ' Seek record.
If MyTable.NoMatch Then
MsgBox "match was not found"
Else
MsgBox "match was found"
End If
End Sub
For more information, please read the Visual Basic online Help for the
Seek, FindFirst, FindLast, FindNext, and FindPrevious Methods.
You can study the database design of a database file such as BIBLIO.MDB by
opening it with Microsoft Access, or with the Data Manager or VISDATA
provided with Visual Basic.
You can run the Data Manager program from the Window menu in Visual Basic,
or from the Windows File Manager run DATAMGR.EXE in the Visual Basic
directory.
The VISDATA.MAK file installed in the VB3\SAMPLES\VISDATA directory loads
extensive examples of data access. The VISDATA sample program uses every
data access function in Visual Basic. You can refer to the VISDATA source
code for examples of how to use each data access function.
Additional query words: 3.00 MoveFirst MoveLast MoveNext MovePrevious
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 23, 1999