ACC2000: How to Fill a Combo Box with Table Names (210311)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210311
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

This article shows you how to create a sample user-defined Visual Basic for Applications function that you can use to fill a combo box or list box with the names of all the tables in the current database.

MORE INFORMATION

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 sample ListAllTables() function.
  1. Start Microsoft Access and open the sample database Northwind.mdb.
  2. Insert a new module.
  3. 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

  4. 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 = ReturnVal
    
    End Function
    					
  5. On the File menu, click Save databasename.
  6. Create a blank new form that is not based on any table or query.
  7. Add a combo box control to the form.
  8. Set the RowSourceType property of the combo box to ListTables.
  9. View the form in Form view. Note that the combo box lists all the tables in the current database.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbProgramming KB210311