Index Object, Indexes Collection Example (MDB)

The following example creates a new index on an Employees table:

Sub NewIndex()
    Dim dbs As Database, tdf As TableDef, idx As Index
    Dim fld1 As Field, fld2 As Field

    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Return reference to Employees table.
    Set tdf = dbs.TableDefs!Employees
    Set idx = tdf.CreateIndex("EmployeeNameIndex")
    Set fld1 = idx.CreateField("LastName")
    Set fld2 = idx.CreateField("FirstName")
    idx.Fields.Append fld1
    idx.Fields.Append fld2
    idx.Required = True
    tdf.Indexes.Append idx
    Set dbs = Nothing
End Sub