>
| Part | Description |
| table | The name of an existing table-type Recordset object that has a defined index as specified by the Recordset object's Index property. |
| comparison | One of the following string expressions: <, <=, =, >=, or >. |
| key1, key2... | One or more values corresponding to fields in the Recordset object's current index, as specified by its Index property setting. |
Dim dbsBiblio As Database, rstPublishers As Recordset
' Open a database.
Set dbsBiblio = DBEngine.Workspaces(0).OpenDatabase("Biblio.mdb")
' Open a table.
Set rstPublishers = dbsBiblio.OpenRecordset("Publishers")
rstPublishers.Index = "PrimaryKey" ' Define current index.
rstPublishers.Seek "=", 3 ' Seek record.
If rstPublishers.NoMatch Then...This example uses the OpenDatabase method to directly open an installable ISAM database and then uses Seek to locate a record in a table in that database.
Dim dbsFoxData as Database, rstParts as Recordset
Dim varSaveHere as Variant
Set dbsFoxData = OpenDatabase("C:\FoxData", False, False,"Fox 2.5")
Set rstParts = dbsFoxData.OpenRecordset("PARTS.dbf", dbOpenTable)
' Choose record order and Seek index.
rstParts.Index = "PartNameIndex"
varSaveHere = rstParts.BookMark ' Save current location.
' Search for first instance of a chosen part.
rstParts.Seek "=", "Framis Lever"
If rstParts.NoMatch then ' Test for success.
rstParts.BookMark = varSaveHere ' Seek not successful.
...
Else ' Seek worked; use current record.
Debug.Print rstParts!PartName
End If
Example (Microsoft Access)
The following example creates a new Index
object on an Employees table. The new index consists of two
fields, LastName and FirstName. The procedure then uses the Seek
method to find a specified record.
Sub NewIndex()
Dim dbs As Database, tdf As TableDef, idx As Index
Dim fldLastName As Field, fldFirstName As Field, rst As Recordset
' Return Database variable pointing to current database.
Set dbs = CurrentDb
Set tdf = dbs.TableDefs!Employees
' Return Index object that points to new index.
Set idx = tdf.CreateIndex("FullName")
' Create and append index fields.
Set fldLastName = idx.CreateField("LastName", dbText)
Set fldFirstName = idx.CreateField("FirstName", dbText)
idx.Fields.Append fldLastName
idx.Fields.Append fldFirstName
' Append Index object.
tdf.Indexes.Append idx
' Open table-type Recordset object.
Set rst = dbs.OpenRecordset("Employees")
' Set current index to new index.
rst.Index = idx.Name
' Specify record to find.
rst.Seek "=", "Fuller", "Andrew"
If rst.NoMatch Then
Debug.Print "Seek unsuccessful!"
Else
Debug.Print "Seek successful."
End If
End Sub
Example (Microsoft Excel)
This example opens PRODUCT.DBF (a dBASE IV table
located in the \Program Files\Common Files\Microsoft
Shared\MSquery folder), locates a record, and then copies the
values into cells B2:C2 on Sheet1. (On Windows NT™,
PRODUCT.DBF is located in the WINDOWS\MSAPPS\MSQUERY folder.)
Const sourceDir = "C:\Program Files\Common Files\Microsoft Shared\" _
& "MSquery"
Dim db As Database, rs As Recordset
Sheets("Sheet1").Activate
Set db = OpenDatabase(sourceDir, False, False, "dBASE IV")
Set rs = db.OpenRecordset("PRODUCT.DBF", dbOpenTable)
rs.Index = "PRODUCT"
rs.Seek "=", "1"
If rs.NoMatch Then
MsgBox "Couldn't find any records"
Else
ActiveSheet.Cells(2, 2) = rs.Fields("CATEGORY").Value
ActiveSheet.Cells(3, 2) = rs.Fields("PROD_NAME").Value
End If
rs.Close
db.Close