>
| Part | Description |
| variable | A variable declared as object data type Index. |
| tabledef | The variable name of the TableDef object you want to use to create the new Index object. |
| name | A String variable that uniquely names the new Index object. See the Name property for details on valid Index names. |
Dim idxAnother As Index, fldLastName As Field
Dim tdfEmployees As TableDef, dbsNorthwind As Database
Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb")
Set tdfEmployees = dbsNorthwind.TableDefs("Employees")
' Create new Index object.
Set idxAnother = tdfEmployees.CreateIndex("Another")
Set fldLastName = idxAnother.CreateField("LastName")
idxAnother.Primary = False
idxAnother.Required = True
idxAnother.Fields.Append fldLastName
' Save Index definition by appending it to Indexes collection.
tdfEmployees.Indexes.Append idxAnother
tdfEmployees.Indexes.Delete "Another"
dbsNorthwind.Close
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.
Sub NewIndex()
Dim dbs As Database, tdf As TableDef
Dim idx As Index, rst As Recordset
Dim fldLastName As Field, fldFirstName As Field
' 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
End Sub