VBA Code to List Objects in Access Database

5
tel 01732 833085 · e-mail david wallis dmw | consultancy HOME DATABASES SPREADSHEETS TEMPLATES CONSULTANCY ABOUT DMW Listing database objects made easy access Q&A excel Q&A technical notes convert DB site map 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. 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 is intended for use in the Immediate Window of the Visual Basic Editoroutputs where it will ouput its list of objects. VBA Function to List All Tables—dmwListAllTables() This function lists all the tables in the database in which you execute it. Function dmwListAllTables() As String 1. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2. Dim tbl As AccessObject, db As Object 3. Dim strMsg As String 4. 5. On Error GoTo Error_Handler 6. VBA Code to List Objects in Access Database http://www.consultdmw.com/access-VBA-list-objects.htm 1 of 5 5/10/2016 4:32 PM

description

VBA

Transcript of VBA Code to List Objects in Access Database

tel 01732 833085 · e-mail david wallis

dmw | consultancy

HOMEDATABASESSPREADSHEETSTEMPLATESCONSULTANCYABOUT DMW

Listing database objects made easy

access Q&Aexcel Q&Atechnical notesconvert DBsite map

VBA Functions to List Access Database ObjectsOn 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 BasicEditor of that database.

If you receive an error message, and assuming the rest of your code compiles without error, then most likelythe error is due to a missing or faulty reference. So check the references, make any adjustments andrecompile.

Each of these functions is intended for use in the Immediate Window of the Visual Basic Editoroutputs whereit will ouput its list of objects.

VBA Function to List All Tables—dmwListAllTables()

This function lists all the tables in the database in which you execute it.

Function dmwListAllTables() As String1. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2. Dim tbl As AccessObject, db As Object3. Dim strMsg As String4.

5. On Error GoTo Error_Handler6.

VBA Code to List Objects in Access Database http://www.consultdmw.com/access-VBA-list-objects.htm

1 of 5 5/10/2016 4:32 PM

7. Set dB = Application.CurrentData8. For Each tbl In db.AllTables9. Debug.Print tbl.Name10. Next tbl11.

12. strMsg = " -- Tables listing complete -- "13.

14. Procedure_Done:15. dmwListAllTables = strMsg16. Exit Function17.

18. Error_Handler:19. strMsg = Err.Number & " " & Err.Description20. Resume Procedure_Done21.

22. End Function23.

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 areusually hidden from the database user's view. Such tables have names beginning with 'MSys'.

VBA Function to Exclude System Tables—dmwListAllTablesNotMSys()

This function lists the tables in your database but in so doing excludes from the list the names of databasesystems tables - those tables beginning 'MSys'.

The difference in code between dmwListAllTablesNotMSys() and dmwListAllTables() is within theFor…Next Loop that starts at line nine.

Function dmwListAllTablesNotMSys() As String1. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2. Dim tbl As AccessObject, dB As Object3. Dim strMsg As String4.

5. On Error GoTo Error_Handler6.

7. Set dB = Application.CurrentData8. For Each tbl In db.AllTables9. If Not Left(tbl.Name, 4) = "MSys" Then10.

VBA Code to List Objects in Access Database http://www.consultdmw.com/access-VBA-list-objects.htm

2 of 5 5/10/2016 4:32 PM

Debug.Print tbl.Name11. End If12. Next tbl13.

14. strMsg = " -- Tables listing complete -- "15.

16. Procedure_Done:17. dmwListAllTablesNotMSys = strMsg18. Exit Function19.

20. Error_Handler:21. strMsg = Err.Number & " " & Err.Description22. Resume Procedure_Done23.

24. End Function25.

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()

This function lists all the queries in the database in which you execute it.

Function dmwListAllQueries() As String1. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2. Dim strMsg As String3. Dim qry As AccessObject, dB As Object4.

5. On Error GoTo Error_Handler6.

7. Set dB = Application.CurrentData8. For Each qry In db.AllQueries9. Debug.Print qry.Name10. Next qry11.

12. strMsg = " -- Queries listing complete -- "13.

14. Procedure_Done:15. dmwListAllQueries = strMsg16. Exit Function17.

18. Error_Handler:19. strMsg = Err.Number & " " & Err.Description20. Resume Procedure_Done21.

22.

VBA Code to List Objects in Access Database http://www.consultdmw.com/access-VBA-list-objects.htm

3 of 5 5/10/2016 4:32 PM

End Function23.

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()

This function lists all the forms in the database in which you execute it.

Function dmwListAllForms() As String1. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2. Dim strMsg As String3. Dim frm As AccessObject, dB As Object4.

5. On Error GoTo Error_Handler6.

7. Set dB = Application.CurrentProject8. For Each frm In db.AllForms9. Debug.Print frm.Name10. Next frm11.

12. strMsg = " -- Forms listing complete -- "13.

14. Procedure_Done:15. dmwListAllForms = strMsg16. Exit Function17.

18. Error_Handler:19. strMsg = Err.Number & " " & Err.Description20. Resume Procedure_Done21.

22. End Function23.

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()

This function lists all the reports in the database in which you execute it.

Function dmwListAllReports() As String1. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2. Dim strMsg As String3.

VBA Code to List Objects in Access Database http://www.consultdmw.com/access-VBA-list-objects.htm

4 of 5 5/10/2016 4:32 PM

Dim rpt As AccessObject, dB As Object4. 5.

On Error GoTo Error_Handler6. 7.

Set dB = Application.CurrentProject8. For Each rpt In dB.AllReports9. Debug.Print rpt.Name10. Next rpt11.

12. strMsg = " -- Reports listing complete -- "13.

14. Procedure_Done:15. dmwListAllReports = strMsg16. Exit Function17.

18. Error_Handler:19. strMsg = Err.Number & " " & Err.Description20. Resume Procedure_Done21.

22. End Function23.

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 livedata.

Copyright © 2010–2016 DMW Consultancy Ltd · Tonbridge · Kent

↑ top

VBA Code to List Objects in Access Database http://www.consultdmw.com/access-VBA-list-objects.htm

5 of 5 5/10/2016 4:32 PM