適用先: Access 2013、Office 2013
関連付けられたテーブルに含まれる Index オブジェクトの一意の値の数を示す値を返します (Microsoft Access ワークスペースのみ)。
構文
式 。DistinctCount
式Index オブジェクトを表す変数。
注釈
インデックスの一意の値または一意のキーの数を確認するには、 DistinctCount プロパティを確認します。 インデックスに重複値が許可されていると、その値が複数個存在する場合もありますが、キーをカウントするのは 1 回のみです。 この情報は、インデックス情報を評価することでデータ アクセスを最適化しようとするアプリケーションで役立ちます。 一意の値の数は、 Index オブジェクトのカーディナリティとも呼ばれます。
DistinctCount プロパティは、特定の時点の実際のキー数を常に反映するわけではありません。 たとえば、ロールバックされたトランザクションによって発生する変更は、 DistinctCount プロパティには直ちに反映されません。 一意のキーを持つレコードの削除も、 DistinctCount プロパティの値に反映されない場合があります。 CreateIndex メソッドを使用した直後は、数値が正確になります。
例
この例では、 DistinctCount プロパティを使用して、 Index オブジェクト内の一意の値の数を決定する方法を示します。 ただし、この値は Index を作成した直後にのみ正確です。 キーが変更されない場合、または新しいキーが追加され、古いキーが削除されない場合は、正確なままになります。そうしないと、信頼性が高くなくなります。 (このプロシージャを複数回実行すると、既存の Index オブジェクトの DistinctCount プロパティ値に対する影響を確認できます)。
Sub DistinctCountX()
Dim dbsNorthwind As Database
Dim tdfEmployees As TableDef
Dim idxCountry As Index
Dim idxLoop As Index
Dim rstEmployees As Recordset
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set tdfEmployees = dbsNorthwind!Employees
With tdfEmployees
' Create and append new Index object to the Employees
' table.
Set idxCountry = .CreateIndex("CountryIndex")
idxCountry.Fields.Append _
idxCountry.CreateField("Country")
.Indexes.Append idxCountry
' The collection must be refreshed for the new
' DistinctCount data to be available.
.Indexes.Refresh
' Enumerate Indexes collection to show the current
' DistinctCount values.
Debug.Print "Indexes before adding new record"
For Each idxLoop In .Indexes
Debug.Print " DistinctCount = " & _
idxLoop.DistinctCount & ", Name = " & _
idxLoop.Name
Next idxLoop
Set rstEmployees = _
dbsNorthwind.OpenRecordset("Employees")
' Add a new record to the Employees table.
With rstEmployees
.AddNew
!FirstName = "April"
!LastName = "LaMonte"
!Country = "Canada"
.Update
End With
' Enumerate Indexes collection to show the modified
' DistinctCount values.
Debug.Print "Indexes after adding new record and " & _
"refreshing Indexes"
.Indexes.Refresh
For Each idxLoop In .Indexes
Debug.Print " DistinctCount = " & _
idxLoop.DistinctCount & ", Name = " & _
idxLoop.Name
Next idxLoop
' Delete new record because this is a demonstration.
With rstEmployees
.Bookmark = .LastModified
.Delete
.Close
End With
' Delete new Indexes because this is a demonstration.
.Indexes.Delete idxCountry.Name
End With
dbsNorthwind.Close
End Sub