ACC2: How to Index an Existing Field with DAOID: Q112107
|
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article demonstrates how to create a compound (multiple- field) index
in an existing table using data access objects (DAO).
This article assumes that you are familiar with Access Basic and with
creating Microsoft Access applications using the programming tools provided
with Microsoft Access. For more information on Access Basic, please refer
to the "Building Applications" manual, Chapter 3, "Introducing Access
Basic."
An Index object contains the fields that are being indexed, and usually
contains only a single field. The Index object created in the example below
will have two fields appended to it, creating a multiple- field index.
The example below demonstrates how to create a multiple- field index with
DAO:
Table: Interviews
--------------------------
Field Name: CustomerID
Data Type: Number
Field Size: Long Integer
Field Name: InterviewerID
Data Type: Number
Field Size: Long Integer
Field Name: Appointment
Data Type: Date/Time
'---------------------------------------------------------------
'PURPOSE: Adds a multiple-field index to a table.
'ACCEPTS: Nothing.
'RETURNS: Nothing.
'---------------------------------------------------------------
Function AddMultiIndex ()
Dim DB As Database, TDef As TableDef
Dim Idx As Index, Fld As Field
Set DB = DBEngine.Workspaces(0).Databases(0)
' Open the table definition.
Set TDef = DB.TableDefs("Interviews")
' Create an index called PrimaryKey for this TableDef
' .. and turn on the Primary and Required properties.
Set Idx = TDef.CreateIndex("PrimaryKey")
Idx.Primary = True
Idx.Required = True
Idx.Ignorenulls = False
' Create an index field with the same name as a table field,
' .. then append it to the index.
Set Fld = Idx.CreateField("CustomerID")
Idx.fields.Append Fld
' Do the second field the same way.
Set Fld = Idx.CreateField("InterviewerID")
Fld.Attributes = DB_DESCENDING
Idx.fields.Append Fld
' Append the index to the TableDef.
TDef.indexes.Append Idx
End Function
For more information about Index objects, search for "Index," and then "Index Object, Indexes Collection" using the Microsoft Access Help menu.
Keywords : kbprg MdlDao PgmObj TblFldp
Version : 2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 2, 1999