>

CreateIndex Method

Applies To

TableDef Object.

Description

Creates a new Index object.

Syntax

Set variable = tabledef.CreateIndex([name])

The CreateIndex method syntax has these parts.

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.


Remarks

If you omit the optional name part when you use CreateIndex, you can use an appropriate assignment statement to set or reset the Name property before you append the new object to a collection. After you append the object, you may or may not be able to set its Name property, depending on the type of object the Indexes collection resides in. See the Name property topic for more details.

If name refers to an object that is already a member of the collection, a trappable error occurs when you use the Append method.

To remove an Index object from a collection, use the Delete method on the collection.

See Also

Append Method, Delete Method, Index Object, Name Property.

Example

This example creates a new Index object.


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