How to Get Access VBA Query to List the Tables in a Database

How to Get Access VBA Query to List the Tables in a Database thumbnail
Access stores information about the database structure in the MSysObjects table.

Microsoft Access is a database management system for individuals to create their own databases, reports and user forms. Access contains all the building blocks for developing small applications. You can create tables for data, stored procedures, user-defined functions and macros in addition to reports and forms. All these components have their own pointers or record definitions stored in a hidden system table. Access keeps track of every table, report, form, etc. you create and stores information such as when it was created or updated and system flags.

Instructions

  1. Getting a List of Tables

    • 1

      Create a query to run the SQL command below. This query retrieves a list of all the tables in the current Access database. The where clause uses the number 1 to indicate that we only want the tables returned.

      SELECT MSysObjects.Name, MSysObjects.DateCreate, MSysObjects.DateUpdate, MSysObjects.Type, MSysObjects.Flags

      FROM MSysObjects

      WHERE MSysObjects.Type = 1

    • 2

      Return a list of only the non-system tables in the database with the example provided:

      SELECT MSysObjects.Name, MSysObjects.DateCreate, MSysObjects.DateUpdate, MSysObjects.Type, MSysObjects.Flags

      FROM MSysObjects

      WHERE MSysObjects.Type = 1 AND MSysObjects.Name Not Like "MSys*"

    • 3

      Return a list of only the non-hidden tables in the database by running the query below:

      SELECT MSysObjects.Name, MSysObjects.DateCreate, MSysObjects.DateUpdate, MSysObjects.Type, MSysObjects.Flags

      FROM MSysObjects

      WHERE MSysObjects.Type = 1 AND MSysObjects.Flags <> 8

    • 4

      Sort the list of non-hidden, non-system tables using the SQL code below:

      SELECT MSysObjects.Name, MSysObjects.DateCreate, MSysObjects.DateUpdate, MSysObjects.Type, MSysObjects.Flags

      FROM MSysObjects

      WHERE MSysObjects.Type = 1 AND MSysObjects.Flags <> 8 AND MSysObjects.Name Not Like "MSys*"

      ORDER BY MSysObjects.Name

    Use the List of Tables in VBA

    • 5

      Load your list of tables into a record set object using the VBA code shown below:

      Dim rsMyTables As DAO.Recordset

      Set rsMyTables = CurrentDb.OpenRecordset("MyQuery")

    • 6

      Loop through each record returned in the query.

      Dim rsMyTables As DAO.Recordset

      Set rsMyTables = CurrentDb.OpenRecordset("MyQuery")

      Do While Not rsMyTables.EOF

      ' Code to manipulate data

      rsMyTables.MoveNext

      Loop

    • 7

      Add each table name to a drop-down list by modifying your while loop:

      Dim rsMyTables As DAO.Recordset

      Set rsMyTables = CurrentDb.OpenRecordset("MyQuery")

      Me.cmbMyComboBox.Clear

      Do While Not rsMyTables.EOF

      Me.cmbMyComboBox.AddItem rsMyTables![Name]

      rsMyTables.MoveNext

      Loop

    • 8

      Close your record set object so it releases the memory it is using:

      Dim rsMyTables As DAO.Recordset

      Set rsMyTables = CurrentDb.OpenRecordset("MyQuery")

      Me.cmbMyComboBox.Clear

      Do While Not rsMyTables.EOF

      Me.cmbMyComboBox.AddItem rsMyTables![Name]

      rsMyTables.MoveNext

      Loop

      rsMyTables.Close

      Set rsMyTables = Nothing

Tips & Warnings

  • Macros are written in Visual Basic for Applications (VBA), which is the Microsoft Standard Office programming language.

  • You can list out any of the objects in Access by changing the "MSysObject.Type" filter. The options for this value are listed below:

  • Table= 1

  • Query= 5

  • Linked Table= 4, 6, or 8

  • Form= -32768

  • Report= -32764

  • Module= -32761

Related Searches:

References

Resources

  • Photo Credit data storage image by Kir from Fotolia.com

Comments

You May Also Like

  • How to Query HTML Tables With VBA Access

    Visual Basic for Applications (VBA) is the programming language for Microsoft's database program Access. VBA can perform the same functions available in...

  • How to Access a 2007 VBA Run Query

    Microsoft Access 2007 is the Windows database program with the graphical query interface. Running queries against Access 2007 databases gives users the...

  • How to Access Word Tables Using Excel VBA

    Word and Excel are both applications in the Microsoft Office suite. Word is a word processing application and Excel a spreadsheet application....

  • How to Export Access Data With VBA

    It's useful to know how to export data from Microsoft Office Access to other applications for further manipulation of data. Access is...

  • How to Refresh a VBA Query in Access

    Knowing how to refresh a Microsoft Access query in Visual Basic for Applications (VBA) is important when you have controls displaying information...

  • How to Access VBA to Get External Data

    Access Visual Basic for Applications (VBA) is the programming language that Microsoft Office developers use to extend Access' features. All commands available...

  • How to Access VBA Reports

    "VBA reports" refer to the reports in a Microsoft Access database that a Visual Basic for Applications (VBA) program manipulates. One operation...

  • Microsoft Access VBA Tutorial

    VBA (Visual Basic for Applications) is part of Microsoft Access, but it uses a different model than VBA in other Microsoft application...

  • How to Build an Access Query With Values From a List

    If you're new to Microsoft Access and want to create a query from an existing table/query list, make use of the query...

Related Ads

Featured