2014年7月6日日曜日

[MS Access] INDEX の定義を一覧表示する

Microsoft Access のインデックス定義を一覧表示するには、DAO.Index のプロパティを参照することで確認できます。
例えば
Sub OutputIndexes()
    Dim TableDef As DAO.TableDef
    Dim Index As DAO.Index

    For Each TableDef In CurrentDb.TableDefs
        If TableDef.Attributes And TableDefAttributeEnum.dbSystemObject Then
            ' システムテーブルは除く
        Else
            For Each Index In TableDef.Indexes
                Debug.Print TableDef.Name & "," & GetIndexDef(Index)
            Next
        End If
    Next
End Sub

Function GetIndexDef(Index As DAO.Index) As String
    Dim Text As String
    Text = Index.Name

    If Index.Primary Then
        Text = Text & ",Primary"
    Else
        Text = Text & ","
    End If
    If Index.Unique Then
        Text = Text & ",Unique"
    Else
        Text = Text & ","
    End If
    If Index.Foreign Then
        Text = Text & ",Foreign"
    Else
        Text = Text & ","
    End If
    If Index.Required Then
        Text = Text & ",Required"
    Else
        Text = Text & ","
    End If
    If Index.IgnoreNulls Then
        Text = Text & ",IgnoreNulls"
    Else
        Text = Text & ","
    End If
    If Index.Clustered Then
        Text = Text & ",Clustered"
    Else
        Text = Text & ","
    End If

    Dim Field As DAO.Field
    For Each Field In Index.Fields
       Text = Text & "," & Field.Name
    Next

    GetIndexDef = Text
End Function

関連する資料のリンク

Index オブジェクト (DAO)
http://msdn.microsoft.com/ja-jp/library/office/ff197655.aspx