How to fill a list box or a combo box with database object names


Advanced: Requires expert coding, interoperability, and multiuser skills.

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

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

For a Microsoft Access 97 version of this article, see
124344 .

Summary


This article shows you how to use the new AddItem method in Microsoft Access to fill a list box or a combo box with the names of database objects.

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 fill a list box or combo box with database object names:
  1. Open the sample database Northwind.mdb.
  2. Create a new form in Design view.
  3. Add a list box control, and then set the following properties:

    Listbox
    -------------------------
    Name: listObjects
    RowSourceType: Value List
    Column Count: 2
    Column Widths: .7"; 1.5"
  4. Add the following code to the form's OnLoad event:
    Private Sub Form_Load()
    Dim accObject As Access.AccessObject

    'Fill with Tables
    For Each accObject In CurrentData.AllTables
    Me.listObjects.AddItem "TABLE;" & accObject.Name
    Next

    'If currently opened file is an Access database (mdb), then fill
    'with queries.
    'Otherwise, if it is an Access project (adp), fill with views,
    'stored procedures, database diagrams, and functions.
    If CurrentProject.ProjectType = acMDB Then
    For Each accObject In CurrentData.AllQueries
    Me.listObjects.AddItem "QUERY;" & accObject.Name
    Next
    Else
    For Each accObject In CurrentData.AllViews
    Me.listObjects.AddItem "VIEW;" & accObject.Name
    Next
    For Each accObject In CurrentData.AllStoredProcedures
    Me.listObjects.AddItem "PROCEDURE;" & accObject.Name
    Next
    For Each accObject In CurrentData.AllDatabaseDiagrams
    Me.listObjects.AddItem "DIAGRAM;" & accObject.Name
    Next
    For Each accObject In CurrentData.AllFunctions
    Me.listObjects.AddItem "FUNCTION;" & accObject.Name
    Next
    End If

    'Fill list with forms.
    For Each accObject In CurrentProject.AllForms
    Me.listObjects.AddItem "FORM;" & accObject.Name
    Next
    'Fill list with reports.
    For Each accObject In CurrentProject.AllReports
    Me.listObjects.AddItem "REPORT;" & accObject.Name
    Next
    'Fill list with data access pages.
    For Each accObject In CurrentProject.AllDataAccessPages
    Me.listObjects.AddItem "PAGE;" & accObject.Name
    Next
    'Fill list with macros.
    For Each accObject In CurrentProject.AllMacros
    Me.listObjects.AddItem "MACRO;" & accObject.Name
    Next
    'Fill list with modules.
    For Each accObject In CurrentProject.AllModules
    Me.listObjects.AddItem "MODULE;" & accObject.Name
    Next
    End Sub
  5. Save the form, and then close it.
  6. Open the form in Form view.
Note that the list box contains two columns. The first column contains the object type, such as table, query, form, and so on. The second column contains the object name.