MS Access VBA – List the Tables in a Database

'---------------------------------------------------------------------------------------
' 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 code may be used as you please, but may not be resold, as
'             long as the header (Author, Website & Copyright) remains with the code.
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' bShowSys - True/False whether or not to include system tables in the list
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2008-June-01            Initial Release
'---------------------------------------------------------------------------------------
Function listTables(bShowSys As Boolean) As String
On Error GoTo listTables_Error
    Dim db As DAO.Database
    Dim td As DAO.TableDefs
 
    Set db = CurrentDb()
    Set td = db.TableDefs
    For Each t In td    'loop through all the fields of the tables
        If Left(t.Name, 4) = "MSys" And bShowSys = False Then GoTo Continue
        Debug.Print t.Name
        'CurrentDb().OpenRecordset ("DELETE * FROM " & t.Name)
Continue:
    Next
 
    Set td = Nothing
    Set db = Nothing
If Err.Number = 0 Then Exit Function
 
listTables_Error:
    MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf & "Error Number: " & _
    Err.Number & vbCrLf & "Error Source: listTable" & vbCrLf & _
    "Error Description: " & Err.Description, vbCritical, "An Error has Occured!"
    Exit Function
End Function

One Comment to “MS Access VBA – List the Tables in a Database”

  1. excellent code.

Leave a Reply









Spam protection by WP Captcha-Free