MS Access List Table Indexes

Once again, trying to help someone in a forum and quickly put together the following to enumerate a list of indexes for a given table:

'---------------------------------------------------------------------------------------
' Procedure : Table_ListIndexes
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : List the names of the indexes in the specified table
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Microsoft Office XX.0 Access database engine Object Library
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sTbl      : Name of the table to list the names of the Indexes from
'
' Usage:
' ~~~~~~
' Call Table_ListIndexes("tbl_Appointments")
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2014-11-20                  Initial Release
' 2         2022-03-25                  Header Usage Correction, per blog comment
' 3         2022-03-25                  Added field enumeration per Brent Spaulding and
'                                       Tom Wickerath's recommendations
'                                       Renamed function, updated header and error
'                                       handler
'---------------------------------------------------------------------------------------
Function Table_ListIndexes(sTbl As String)
On Error GoTo Error_Handler
    Dim db              As DAO.Database
    Dim tdf             As DAO.TableDef
    Dim tIndex          As DAO.Index
    Dim fld             As DAO.Field

    Set db = CurrentDb
    Set tdf = db.TableDefs(sTbl)

    For Each tIndex In tdf.Indexes
        Debug.Print tIndex.Name
        For Each fld In tIndex.Fields
           Debug.Print , fld.Name
        Next
    Next

Error_Handler_Exit:
    On Error Resume Next
    If Not fld Is Nothing Then Set fld = Nothing
    If Not tIndex Is Nothing Then Set tIndex = Nothing
    If Not tdf Is Nothing Then Set tdf = Nothing
    If Not db Is Nothing Then Set db = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Source: Table_ListIndexes" & vbCrLf & _
               "Error Description: " & Err.Description & _
               Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
               , vbOKOnly + vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Function

2 responses on “MS Access List Table Indexes

  1. BG

    You have a typo in your usage example, it should be:
    Call ListTblIndexes(“tbl_Appointments”)