MS Access VBA – List the Tables in a Database

The following function will enumerate all the tables within an Access database to the VBE Immediate Window. You have the option, using the bShowSys input variable, to include or exclude system tables from the returned list.

'---------------------------------------------------------------------------------------
' Procedure : listTables
' Author    : CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Return a listing of all the tables in the database
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' bShowSys - True/False whether or not to include system tables in the list.  If omitted
'               it defaults to False, omitting to list system tables.
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2008-06-01              Initial Release
' 2         2020-12-12              Updated copyright
'                                   Updated error handler
'                                   Made Option Explicit compliant
'---------------------------------------------------------------------------------------
Function listTables(Optional bShowSys As Boolean = False) As String
    Dim db                    As DAO.Database
    Dim tdfs                  As DAO.TableDefs
    Dim tdf                   As DAO.TableDef

    On Error GoTo Error_Handler

    Set db = CurrentDb()
    Set tdfs = db.TableDefs
    For Each tdf In tdfs    'loop through all the fields of the tables
        If (Left(tdf.Name, 4) = "MSys" Or Left(tdf.Name, 1) = "~") _
            And bShowSys = False Then GoTo Continue
        Debug.Print tdf.Name
Continue:
    Next

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

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

6 responses on “MS Access VBA – List the Tables in a Database

  1. Hank Reed

    Works perfectly. I only wanted to see the names of local tables so I check the .Connect field
    It’s empty for local tables. Has data for externally linked tables

  2. Michael D Krailo

    Variable t is not dimensioned in the listed code. What type is it? I just set it to a variant since you did not define it.