>
Dim rstTitles as Recordset, dbsBiblio as Database Set dbsBiblio = Workspaces(0).OpenDatabase("Biblio.mdb") Set rstTitles = dbsBiblio.OpenRecordset("Titles",dbOpenTable) rstTitles.Index = "PubID" rstTitles.Seek "=", 3The specified Index object must already be defined. If you set the Index property to an Index object that doesn't exist or if the Index property isn't set when you use the Seek method, a trappable error occurs. Examine the Indexes collection of a TableDef object to determine what Index objects are available to table-type Recordset objects created from that TableDef object. You can create a new index for the table by creating a new Index object, setting its properties, appending it to the Indexes collection of the underlying TableDef object, and then reopening the Recordset object. Records returned from a table-type Recordset object can be ordered only by the indexes defined for the underlying TableDef object. To sort records in some other order, you can open a dynaset- or Snapshot-Type Recordset object using an SQL statement with an ORDER BY clause. Tips
Dim rstPublishers As Recordset, dbsBiblio As Database Dim idxPubIDName As Index, fldPubID As Field, fldName As Field Set dbsBiblio = DBEngine.Workspaces(0).OpenDatabase("Biblio.mdb") Set idxPubIDName = dbsBiblio.TableDefs("Publishers").CreateIndex _ ("PubID Name") Set fldPubID = idxPubIDName.CreateField("PubID") idxPubIDName.Fields.Append fldPubID Set fldName = idxPubIDName.CreateField("Name") idxPubIDName.Fields.Append fldName idxPubIDName.Unique = True Debug.Print idxPubIDName.Fields ' Show Fields property. ' Append the new Index to the Indexes collection. dbsBiblio.TableDefs("Publishers").Indexes.Append idxPubIDName 'Open the table. Set rstPublishers = dbsBiblio.OpenRecordset("Publishers",dbOpenTable) rstPublishers.Index = "PubID Name" ' Set table's current index. rstPublishers.Seek "=", "27", "Yourdon Press" ' Look for Publisher. If rstPublishers.NoMatch Then rstPublishers.MoveFirst Debug.Print "Not found" Else Debug.Print rstPublishers!Name End If
rstPublishers.CloseThis example sets the index for rstOrders to the primary key. (PrimaryKey is the default name of an Index object if the primary key is set in the table's Design view.) Next, it locates the record with a matching key field value of 10,050. Notice that the current index must be set before certain operations, such as the Seek method, can be performed.
Dim dbsNorthwind As Database, rstOrders As Recordset Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb") Set rstOrders = dbsNorthwind.OpenRecordset("Orders", dbOpenTable) rstOrders.Index = "PrimaryKey" ' Set current index. rstOrders.Seek "=", 10050 ' Locate record. ... rstOrders.Close ' Close recordset. dbsNorthwind.Close ' Close database.Example (Microsoft Access) The following example sets the index for a table-type Recordset object to the primary key. "PrimaryKey" is the default name of an Index object if the index corresponds to the primary key set in table Design view. Next, the procedure asks the user for a value to search on and locates the record with a matching key field value. Note that the current index must be set before certain operations, such as the Seek method, can be performed on a table-type Recordset object.
Sub UsePrimaryKey() Dim dbs As Database, rst As Recordset Dim fld as Field, strInput as String ' Return Database variable that points to current database. Set dbs = CurrentDb Set rst = dbs.OpenRecordset("Orders", dbOpenTable) ' Set current index. rst.Index = "PrimaryKey" strInput = InputBox("Enter the OrderID on which to search.") ' Locate record. rst.Seek "=", strInput If Not rst.NoMatch Then For Each fld in rst.Fields Debug.Print fld.Name; " "; fld.Value Next fld End If ' Close Recordset object. rst.Close End Sub