次の方法で共有


TableDef.CreateIndex メソッド (DAO)

適用先: Access 2013、Office 2013

新しい Index オブジェクトを作成します (Microsoft Access ワークスペースのみ)。 .

構文

。CreateIndex(Name)

TableDef オブジェクトを表す変数。

パラメーター

名前

必須かどうか

データ型

説明

名前

省略可能

バリアント型 (Variant)

新しい Index オブジェクトの一意の名前を表す文字列型 (String) の値。 有効な Index 名の詳細については、Name プロパティを参照してください。

戻り値

インデックス

注釈

CreateIndex メソッドを使用して、 TableDef オブジェクトの新しい Index オブジェクトを作成できます。 CreateIndex を使用するときに省略可能な名前部分を省略した場合は、新しいオブジェクトをコレクションに追加する前に、適切な割り当てステートメントを使用して Name プロパティを設定またはリセットできます。 オブジェクトの追加後に Name プロパティを設定できるかどうかは、 Indexes コレクションを格納しているオブジェクトの種類によって決まります。 詳細については、 Name プロパティのトピックを参照してください。

name が既にコレクションのメンバーであるオブジェクトを参照している場合、 Append メソッドを使用すると実行時エラーが発生します。

コレクションから Index オブジェクトを削除するには、コレクションの Delete メソッドを使用します。

次の使用例では、 CreateIndex メソッドを使用して 2 つの新しい 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