CreateIndex Method Example (MDB)
The following example creates a new Index object on an Employees table. The new index consists of two fields, LastName and FirstName.
Sub NewIndex()
Dim dbs As Database, tdf As TableDef
Dim idx As Index
Dim fldLastName As Field, fldFirstName As Field
' Return reference to current database.
Set dbs = CurrentDb
' Return reference to Employees table.
Set tdf = dbs.TableDefs!Employees
' Return reference 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 and refresh collection.
tdf.Indexes.Append idx
tdf.Indexes.Refresh
Set dbs = Nothing
End Sub