This example shows how to programmatically create:
- a primary key index;
- an index on a single field;
- a multi-field index.
Sub CreateIndexesDAO()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim ind As DAO.Index
'Initialize
Set db = CurrentDb()
Set tdf = db.TableDefs("tblContractor")
'1. Primary key index.
Set ind = tdf.CreateIndex("PrimaryKey")
With ind
.Fields.Append .CreateField("ContractorID")
.Unique = False
.Primary = True
End With
tdf.Indexes.Append ind
'2. Single-field index.
Set ind = tdf.CreateIndex("Inactive")
ind.Fields.Append ind.CreateField("Inactive")
tdf.Indexes.Append ind
'3. Multi-field index.
Set ind = tdf.CreateIndex("FullName")
With ind
.Fields.Append .CreateField("Surname")
.Fields.Append .CreateField("FirstName")
End With
tdf.Indexes.Append ind
'Refresh the display of this collection.
tdf.Indexes.Refresh
End Sub

Signature
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
> How can I add an index to an existing field using VB?
>
> Thanks!
Jaden - 20 Jan 2005 17:45 GMT
Hey Allen -
Works like a charm... I had located the example in Help but I had my syntax
messed up until I saw your reply. Thanks for your assistance.
> This example shows how to programmatically create:
> - a primary key index;
[quoted text clipped - 40 lines]
> >
> > Thanks!