ACC2000: How to Fill a Combo Box with Table Names

ID: Q210311


The information in this article applies to:
  • Microsoft Access 2000

Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and 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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
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:


  5. 
    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
    
       ListAllTables = ReturnVal
    
    End Function 
  6. On the File menu, click Save databasename.


  7. Create a blank new form that is not based on any table or query.


  8. Add a combo box control to the form.


  9. Set the RowSourceType property of the combo box to ListTables.


  10. View the form in Form view. Note that the combo box lists all the tables in the current database.


Additional query words: tablenames

Keywords : kbprg kbdta AccCon
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 6, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.