The information in this article applies to:
- Microsoft Access version 2.0
SUMMARY
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."
MORE INFORMATION
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:
- Open the sample database NWIND.MDB.
- Create the following new table:
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
- Save the table as Interviews without creating a primary key, and then
close it.
- Create a new module and type the following line in the Declarations
section:
Option Explicit
- Type the following function in the module:
'---------------------------------------------------------------
'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
- Save the module as DAOaddMultiIndex.
- From the View menu, choose Immediate Window.
- Type the following in the Immediate window, and then press ENTER:
? AddMultiIndex()
- Close the Immediate window and the module, and then open the Interviews
table in Design view. Note the compound primary key on the CustomerID
and InterviewerID fields.
REFERENCES
For more information about Index objects, search for "Index," and then
"Index Object, Indexes Collection" using the Microsoft Access Help menu.
Keywords : MdlDao PgmObj TblFldp kbprg
Version : 2.0
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto