TDef.Indexes.Append Idx
End Sub
This code sets up the relevant objects using theDimstatement. Note that you must create
a database object. You cannot refer directly to theCurrentDbobject.
The table definition object is set to point to the table NewTable that you created earlier.
The index object is used to create an index on that table definition called MyIndex.
A field called LongIntegerField is then created in the index. This relates to a field already
created in the table. Note that this creates the field in the index, not in the table.
Properties for Primary (making it the key field), Required, and Unique are set to True.
The index is then appended to the index collection for that table definition.
Run this code and then view the table NewTable in Design mode. You will see that the
field LongIntegerField is now a key field and is indexed according to the properties stated
earlier.
To remove the index from the table, use the command:
CurrentDb.TableDefs("NewTable").Indexes.Delete "MyIndex"
316 Microsoft Access 2010 VBA Macro Programming