>
Set dbsBiblio = Workspaces(0).OpenDatabase("Biblio.mdb")
Set rstPublishers = dbsBiblio.OpenRecordset("Publishers", dbOpenTable)
rstPublishers.Index = "ZIP_INDEX"
Tip
You don't have to create
indexes for a table, but for large, unindexed tables, accessing a
specific record or processing joins can take a long time.
Conversely, having too many indexes can slow down updates to the
database as each of the table indexes is amended.
The Attributes property
of each Field object in the index determines the order of
records returned and consequently determines which access
techniques to use for that index.
Each Field object in the
Fields collection of an Index object is a component
of the index. You define a new Index object by setting its
properties before you append it to a collection, which makes the Index
object available for subsequent use.
Note
You can modify the Name
property setting of an existing Index object only if the Updatable
property setting of the containing TableDef object is True.
Using an Index object
and its properties, you can:
Function EnumerateIndex () As Integer
Dim dbsDefault As Database, tdfTest As TableDef
Dim fldOne As Field, fldTwo As Field, idxPrimary As Index
Dim I As Integer
' Get workspace and database.
Set dbsDefault = _
DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb")
' Create table with two fields.
Set tdfTest = dbsDefault.CreateTableDef("MyTable")
Set fldOne = tdfTest.CreateField("Field1", dbLong)
fldOne.Required = True ' No Null values allowed.
tdfTest.Fields.Append fldOne
Set fldTwo = tdfTest.CreateField("Field2", dbLong)
fldTwo.Required = True ' No Null values allowed.
tdfTest.Fields.Append fldTwo
dbsDefault.TableDefs.Append tdfTest
' Create primary index for those two fields.
Set idxPrimary = tdfTest.CreateIndex("MyIndex")
idxPrimary.Primary = True
Set fldOne = tdfTest.CreateField("Field1")
idxPrimary.Fields.Append fldOne
Set fldTwo = tdfTest.CreateField("Field2")
idxPrimary.Fields.Append fldTwo
tdfTest.Indexes.Append idxPrimary
' Enumerate index and its fields.
Debug.Print "Index: "; idxPrimary.Name
Debug.Print " Required: "; idxPrimary.Required
Debug.Print " IgnoreNulls: "; idxPrimary.IgnoreNulls
Debug.Print " Primary: "; idxPrimary.Primary
Debug.Print " Clustered: "; idxPrimary.Clustered
Debug.Print " Unique: "; idxPrimary.Unique
Debug. Print " Foreign: "; idxPrimary.Foreign
Debug.Print
Debug.Print "Fields in Index: ";
For I = 0 To idxPrimary.Fields.Count - 1
Debug.Print " "; idxPrimary.Fields(I).Name;
Next I
Debug.Print
EnumerateIndex = True
End Function
This example opens a table-type
Recordset and selects an index for the Recordset.
By setting an index, the Microsoft Jet database engine returns
records in the order specified by the index. Without an index,
table-type Recordset objects return records from the
database table in no particular order.
Dim dbsDefault As Database
Dim rstTitles as Recordset
' Get workspace and database.
Set dbsDefault = _
DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb")
Set rstTitles = dbsDefault.OpenRecordset("Titles")
rstTitles.Index = "MyIndex"
.
.
.
Example (Microsoft
Access)
The following example creates a
new index on an Employees table.
Sub NewIndex()
Dim dbs As Database, tdf As TableDef, idx As Index
Dim fld As Field
' Return Database object pointing to current database.
Set dbs = CurrentDb
Set tdf = dbs.TableDefs!Employees
Set idx = tdf.CreateIndex("LastNameIndex")
Set fld = idx.CreateField("LastName")
idx.Fields.Append fld
tdf.Indexes.Append idx
End Sub