• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Creating an Index

Indexes are created using the CreateIndex method for a TableDef object. Here is the syntax:

Set IndexVar = TableDefVar.CreateIndex([IndexName])

Creating an index by itself does nothing. We must append one or more fields to the Fields collection of the index in order to actually index the table. Moreover, the order in which the fields are appended (when there is more than one field) has an effect on the index order. This is demonstrated in Example 15-3, in which a new index called PriceTitle is added to the BOOKS table.

Example 15-3. A CreateIndex method example
Sub exaCreateIndex(  )

Dim db As DATABASE
Dim tdf As TableDef
Dim idx As INDEX
Dim fld As Field

Set db = CurrentDb
Set tdf = db.TableDefs!BOOKS

' Create index by the name of PriceTitle
Set idx = tdf.CreateIndex("PriceTitle")

' Append the price and then the Title fields
' to the Fields collection of the index
Set fld = idx.CreateField("Price")
idx.Fields.Append fld
Set fld = idx.CreateField("Title")
idx.Fields.Append fld

' Append the index to the indexes collection 
' for BOOKS
tdf.Indexes.Append idx

End 
                  Sub

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint