This article was previously published under Q210311
This article has been archived. It is offered "as is" and will no longer be updated.
Moderate: Requires basic macro, coding, and interoperability skills.
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
This article shows you how to create a sample user-defined Visual Basic forApplications function that you can use to fill a combo box or list boxwith the names of all the tables in the current database.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following steps demonstrate how to create and use the sampleListAllTables() function.
Start Microsoft Access and open the sample database Northwind.mdb.
Insert a new module.
On the Tools menu, click References, and select the following references if they are not already selected (checked):
Microsoft ActiveX Data Objects 2.1 Library
Microsoft ADO Ext. 2.1 for DDL and Security
Type the following code in the module:
Function ListTables(fld As Control, id As Long, row As Long, _ col As Long, code As Integer) Dim cat As New ADOX.Catalog Dim tbl As ADOX.Table Static tbls(256) As String Static Entries As Integer Dim ReturnVal ReturnVal = Null Select Case code Case acLBInitialize ' Initialize database. cat.ActiveConnection = CurrentProject.Connection Entries = 0 For Each tbl In cat.Tables If tbl.Type = "TABLE" Then If tbl.Name <> "dtproperties" Then tbls(Entries) = tbl.Name Entries = Entries + 1 End If End If Next tbl ReturnVal = Entries Case acLBOpen ' Open. ReturnVal = Timer ' Unique ID number for control. Case acLBGetRowCount ' Number of rows. ReturnVal = Entries Case acLBGetColumnCount ' Number of columns. ReturnVal = 1 Case acLBGetColumnWidth ' Column width. ReturnVal = -1 ' Use the default width. Case acLBGetValue ' Get the data. ReturnVal = tbls(row) Case acLBEnd ' End. For Entries = 0 To 256 tbls(Entries) = "" Next End Select ListTables = ReturnValEnd Function
On the File menu, click Save databasename.
Create a blank new form that is not based on any table or query.
Add a combo box control to the form.
Set the RowSourceType property of the combo box to ListTables.
View the form in Form view. Note that the combo box lists all the tables in the current database.