ACC: How to Create a Collection of Collections

Last reviewed: August 28, 1997
Article ID: Q154448
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

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

One of the limitations of the Visual Basic for Applications Collection object is that it cannot contain a user-defined data type. Similar behavior can be accomplished, however, by creating a collection that contains the elements you would normally define in your user-defined data type, and then storing that collection in a second collection. This article provides an example of how to create "nested" collections.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

MORE INFORMATION

To create a collection within a collection, follow these steps:

  1. Create a module and type the following procedure:

          Function CollectionCollection()
    
             Dim element
             Dim insideCollection As New Collection
             Dim outsideCollection As New Collection
    
             ' Populate the first element of the collection and add
             ' it to the parent collection.
             insideCollection.Add KEY:="fname", Item:="joe"
             insideCollection.Add KEY:="lname", Item:="smith"
             insideCollection.Add KEY:="age", Item:=12
             outsideCollection.Add insideCollection
    
             ' Clear the collection - prevents duplication of elements.
             Set insideCollection = Nothing
    
             ' Populate the second element of the collection and add
             ' it to the parent collection.
             insideCollection.Add KEY:="fname", Item:="fred"
             insideCollection.Add KEY:="lname", Item:="smith"
             insideCollection.Add KEY:="age", Item:=14
             outsideCollection.Add insideCollection
    
             ' Print the contents of the parent collection to the Debug window.
             For Each element In outsideCollection
                Debug.Print element("fname"), element("lname"), element("age")
             Next
          End Function
    
    

  2. To test the function, type the following in the Debug window, and then press ENTER:

          CollectionCollection
    

    Note that the contents of the collection appears in the Debug window.

REFERENCES

For more information about collections, search the Help Index for "Collection object.

Keywords          : PgmColl PgmHowTo PgmObj
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.