TableDef.CreateIndex 方法 (DAO)

适用于:Access 2013、Office 2013

仅) (Microsoft Access 工作区创建新的 Index 对象。 .

语法

表达式 。CreateIndex (名称)

表达式 一个表示 TableDef 对象的变量。

参数

名称

必需/可选

数据类型

说明

Name

可选

Variant

一个对新的 Index 对象进行唯一命名的 String。 有关有效 Index 名称的详细信息,请参阅 Name 属性。

返回值

索引

备注

您可以使用 CreateIndex 方法为 TableDef 对象创建一个新的 Index 对象。 如果使用 CreateIndex 时省略了可选名称部分,则可以使用适当的赋值语句在将新对象追加到集合之前设置或重置 Name 属性。 追加对象后,您也许能够或不能设置其 Name 属性(取决于包含 Indexes 集合的对象类型)。 有关详细信息,请参阅 Name 属性主题。

如果 name 引用已是集合成员的对象,则使用 Append 方法时将发生运行时错误。

要从集合中删除 Index 对象,请对集合使用 Delete 方法。

示例

以下示例使用 CreateIndex 方法创建两个新的 Index 对象,然后将它们追加到 Employees TableDef 对象的 Indexes 集合。 然后,该示例枚举 TableDef 对象的 Indexes 集合、新 Index 对象的 Fields 集合,以及新 Index 对象的 Properties 集合。 若要使该过程正常运行,需要使用 CreateIndexOutput 函数。

    Sub CreateIndexX() 
     
     Dim dbsNorthwind As Database 
     Dim tdfEmployees As TableDef 
     Dim idxCountry As Index 
     Dim idxFirstName As Index 
     Dim idxLoop As Index 
     
     Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
     Set tdfEmployees = dbsNorthwind!Employees 
     
     With tdfEmployees 
     ' Create first Index object, create and append Field 
     ' objects to the Index object, and then append the 
     ' Index object to the Indexes collection of the 
     ' TableDef. 
     Set idxCountry = .CreateIndex("CountryIndex") 
     With idxCountry 
     .Fields.Append .CreateField("Country") 
     .Fields.Append .CreateField("LastName") 
     .Fields.Append .CreateField("FirstName") 
     End With 
     .Indexes.Append idxCountry 
     
     ' Create second Index object, create and append Field 
     ' objects to the Index object, and then append the 
     ' Index object to the Indexes collection of the 
     ' TableDef. 
     Set idxFirstName = .CreateIndex 
     With idxFirstName 
     .Name = "FirstNameIndex" 
     .Fields.Append .CreateField("FirstName") 
     .Fields.Append .CreateField("LastName") 
     End With 
     .Indexes.Append idxFirstName 
     
     ' Refresh collection so that you can access new Index 
     ' objects. 
     .Indexes.Refresh 
     
     Debug.Print .Indexes.Count & " Indexes in " & _ 
     .Name & " TableDef" 
     
     ' Enumerate Indexes collection. 
     For Each idxLoop In .Indexes 
     Debug.Print " " & idxLoop.Name 
     Next idxLoop 
     
     ' Print report. 
     CreateIndexOutput idxCountry 
     CreateIndexOutput idxFirstName 
     
     ' Delete new Index objects because this is a 
     ' demonstration. 
     .Indexes.Delete idxCountry.Name 
     .Indexes.Delete idxFirstName.Name 
     End With 
     
     dbsNorthwind.Close 
     
    End Sub 
     
    Function CreateIndexOutput(idxTemp As Index) 
     
     Dim fldLoop As Field 
     Dim prpLoop As Property 
     
     With idxTemp 
     ' Enumerate Fields collection of Index object. 
     Debug.Print "Fields in " & .Name 
     For Each fldLoop In .Fields 
     Debug.Print " " & fldLoop.Name 
     Next fldLoop 
     
     ' Enumerate Properties collection of Index object. 
     Debug.Print "Properties of " & .Name 
     For Each prpLoop In .Properties 
     Debug.Print " " & prpLoop.Name & " - " & _ 
     IIf(prpLoop = "", "[empty]", prpLoop) 
     Next prpLoop 
     End With 
     
    End Function