VBA functions to list access database objects
On this page are VBA functions that list the tables, queries, forms and reports in the current database.
DMW uses these functions during the development phases of Access 2003 and Access 2007 databases.
To include any one of these functions in your database, copy and paste it into a module in the Visual Basic Editor of that database, edit out the line numbers shown here in the code listing, and then compile the module.
If you receive an error message, and assuming the rest of your code compiles without error, then most likely the error is due to a missing or faulty reference. So check the references, make any adjustments and recompile.
Each of these functions outputs its list to the Immediate Window in the Visual Basic Editor.
VBA function to list all tables
dmwListAllTables() function
This function lists all the tables in the database in which you execute it.
VBA function code
- Function dmwListAllTables() As String
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Dim tbl As AccessObject, db As Object
- Dim strMsg As String
-
- On Error GoTo Error_Handler
-
- Set dB = Application.CurrentData
- For Each tbl In db.AllTables
- Debug.Print tbl.Name
- Next tbl
-
- strMsg = " -- Tables listing complete -- "
-
- Procedure_Done:
- dmwListAllTables = strMsg
- Exit Function
-
- Error_Handler:
- strMsg = Err.Number & " " & Err.Description
- Resume Procedure_Done
-
- End Function
to execute the VBA code
To run the code, input ?dmwListAllTables() into the Immediate Window and press Enter.
comments
Note that the list of tables that dmwListAllTables()
generates includes the names of system tables that are usually hidden
from the database user's view. Such tables have names beginning with 'MSys'.
VBA function to exclude system tables
dmwListAllTablesNotMSys() function
This function lists the tables in your database but in so doing excludes from the list the names of database systems tables — those tables beginning 'MSys'.
The difference in code between dmwListAllTablesNotMSys() and dmwListAllTables() is within the For…Next Loop that starts at line nine.
VBA function code
- Function dmwListAllTablesNotMSys() As String
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Dim tbl As AccessObject, dB As Object
- Dim strMsg As String
-
- On Error GoTo Error_Handler
-
- Set dB = Application.CurrentData
- For Each tbl In db.AllTables
- If Not Left(tbl.Name, 4) = "MSys" Then
- Debug.Print tbl.Name
- End If
- Next tbl
-
- strMsg = " -- Tables listing complete -- "
-
- Procedure_Done:
- dmwListAllTablesNotMSys = strMsg
- Exit Function
-
- Error_Handler:
- strMsg = Err.Number & " " & Err.Description
- Resume Procedure_Done
-
- End Function
to execute the VBA code
To run the code, input ?dmwListAllTablesNotMSys() into the Immediate Window and press Enter.
VBA function to list all queries
dmwListAllQueries() function
This function lists all the queries in the database in which you execute it.
- Function dmwListAllQueries() As String
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Dim strMsg As String
- Dim qry As AccessObject, dB As Object
-
- On Error GoTo Error_Handler
-
- Set dB = Application.CurrentData
- For Each qry In db.AllQueries
- Debug.Print qry.Name
- Next qry
-
- strMsg = " -- Queries listing complete -- "
-
- Procedure_Done:
- dmwListAllQueries = strMsg
- Exit Function
-
- Error_Handler:
- strMsg = Err.Number & " " & Err.Description
- Resume Procedure_Done
-
- End Function
to execute the VBA code
To run the code, input ?dmwListAllQueries() into the Immediate Window and press Enter.
VBA function to list all forms
dmwListAllForms() function
This function lists all the forms in the database in which you execute it.
- Function dmwListAllForms() As String
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Dim strMsg As String
- Dim frm As AccessObject, dB As Object
-
- On Error GoTo Error_Handler
-
- Set dB = Application. CurrentProject
- For Each frm In db.AllForms
- Debug.Print frm.Name
- Next frm
-
- strMsg = " -- Forms listing complete -- "
-
- Procedure_Done:
- dmwListAllForms = strMsg
- Exit Function
-
- Error_Handler:
- strMsg = Err.Number & " " & Err.Description
- Resume Procedure_Done
-
- End Function
to execute the VBA code
To run the code, input ?dmwListAllForms() into the Immediate Window and press Enter.
VBA function to list all reports
dmwListAllReports() function
This function lists all the reports in the database in which you execute it.
- Function dmwListAllReports() As String
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Dim strMsg As String
- Dim rpt As AccessObject, dB As Object
-
- On Error GoTo Error_Handler
-
- Set dB = Application. CurrentProject
- For Each rpt In dB AllReports
- Debug.Print rpt.Name
- Next rpt
-
- strMsg = " -- Reports listing complete -- "
-
- Procedure_Done:
- dmwListAllReports = strMsg
- Exit Function
-
- Error_Handler:
- strMsg = Err.Number & " " & Err.Description
- Resume Procedure_Done
-
- End Function
to execute the VBA code
To run the code, input ?dmwListAllReports() into the Immediate Window and press Enter.
disclaimer
DMW Consultancy Limited does not accept
any liability for loss or damage to data to which any techniques, methods or code included in this website are applied. Back up your data; test thoroughly before
using on live data.