Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Use Collections to Manage Class Objects in VBA


View products that this article applies to.

Summary

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

This article shows you how to use collections in Visual Basic for Applications to manage references to class objects in Access 2000. This technique allows your class objects to persist, and enables you to control the individual properties of those objects by using the familiar collection syntax used in Microsoft Access for implementing Data Access Objects (DAO) and other Microsoft Office Object models.

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.

↑ Back to the top


More information

In order to use collections to manage class objects, you must do the following:
  • Create an instance of the class
  • Set the properties and methods of the class
  • Add the class to a public collection
  • Unload the instance of the class
You might expect that unloading the instance of the class results in the class being closed and terminated. However, the class object persists because you add it to a collection, which then owns the reference to the class. This is a very powerful technique that allows you to control object references through a collection; the class object does not terminate until you remove it from the collection.

The following example creates a class object and a form object, and then manages both objects from a collection in a standard module.

Create a Class Module

  1. Create a new database called ClassTest.mdb.
  2. On the Insert menu, click Class Module.
  3. Save the class module as clsTest.
  4. Type the following lines in the Declarations section:
      Private This_ClassID As String
      Private This_frm As New Form_frmTest
    
    					
  5. Select Class in the Object box of the Module window. "Initialize" is automatically selected in the Procedure box.
  6. Type the following procedure:
    Private Sub Class_Initialize()
       On Local Error GoTo Class_Initialize_Err
       Dim Msg As String
       This_frm.Visible = True
       This_ClassID = "Initialized"
       This_frm.Caption = This_ClassID
       MsgBox "Class Initialized", vbInformation, "Class Example"
    Class_Initialize_End:
       Exit Sub
    Class_Initialize_Err:
       Msg = "Error #: " & Format$(Err.Number) & vbCrLf
       Msg = Msg & Err.Description
       Err.Raise vbObjectError, "clsTest.Initialize (Private)", Msg
       Resume Class_Initialize_End
    End Sub
    					
  7. On the Insert menu, click Procedure.
  8. In the Insert Procedure dialog box, type ClassID in the Name box and click Property in the Type box. Then type the following procedures
      Public Property Get ClassID() As Variant
         ClassID = This_ClassID
      End Property
    
      Public Property Let ClassID(ByVal vNewValue As Variant)
         This_ClassID = vNewValue
         This_frm.ClassID = This_ClassID
         This_frm.Caption = This_ClassID
      End Property
    
    					
  9. Save and close the clsTest class module.

Create a Form

  1. Create the following form not based on any table or query in Design view:
       Form: frmTest
       -----------------
       Caption: TestForm
    					
  2. With the form still open in Design view, click Code on the View menu.
  3. Type the following line in the Declarations section of the form's class module:
    Dim This_ClassID As String
    					
  4. Add the following event procedure to the form's Unload property:
    Private Sub Form_Unload(Cancel As Integer)
        col.Remove This_ClassID
    End Sub
    					
  5. On the Insert menu, click Procedure.
  6. In the Insert Procedure dialog box, type ClassID in the Name box and click Property in the Type box. Then type the following procedures. Note that the Get ClassID() function and vNewValue variable in this example are dimensioned as String instead of the default, which is Variant:
    Public Property Get ClassID() As String
       ClassID = This_ClassID
    End Property
    
    Public Property Let ClassID(ByVal vNewValue As String)
       This_ClassID = vNewValue
    End Property
    					
  7. Save and close the frmTest form.

Create a Standard Module

  1. Create a new standard module and save it as Module1.
  2. Type the following line in the Declarations section:
    Public col As New Collection
    					
  3. Type the following procedure:
    Function CreateClassTest() As String
       ' Create an instance of the clsTest class module, which creates
       ' an instance of the frmTest form.
       Dim cls As New clsTest
    
       ' Create a unique identifier string and set it to the upper index
       ' of the Public col Collection plus one.
       Dim varClassId As String
       varClassId = "Key_" & CStr(col.Count + 1)
    
       ' Set the clsTest class module's ClassID property to the value of
       ' varClassId, which in turn sets the frmTest.ClassId property to
       ' the same value. This is so the form has a method to track its
       ' relationship to the collection.
       cls.ClassID = varClassId
    
       ' Add the instance of the class object to the collection passing
       ' varClassId as the Key argument.
       col.Add cls, varClassId
    
       MsgBox "Created New Collection Item: " & varClassId, _
            vbInformation, "Class Example"
    
       ' Unload the cls object variable.
       Set cls = Nothing
    
       ' Return the varClassId.
       CreateClassTest = varClassId
    End Function
    					
  4. Close and save the module.

Test the Example

When you call the CreateClassTest() function multiple times, it opens multiple instances of the frmTest form, each of which is unique and capable of managing itself and its participation in the public collection. Each form is aware of its Key position in the collection, and each one removes itself from the collection when you close the form.

The following sample procedure creates three instances of the clsTest class:
  1. Create a standard module and type the following procedure:
    Function CreateThreeItems() As Boolean
       Dim strKeys(1 To 3) As String
       Dim i As Integer
       For i = LBound(strKeys) To UBound(strKeys)
          strKeys(i) = CreateClassTest()
       Next i
       For i = LBound(strKeys) To UBound(strKeys)
          MsgBox col.Item(strKeys(i)).ClassID, vbInformation, _
            "Class Test"
       Next i
    End Function
    					
  2. To test this function, type the following line in the Immediate window, and then press ENTER:
     ?CreateThreeItems()
    						
    Note that messages boxes are displayed each time the clsTest class module initializes, when each of three instances of the frmTest form is created, and again after all three instances of the form are open.

↑ Back to the top


References

For more information about class modules, click Microsoft Access Help on the Help menu, type "class modules" in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

For more information about the properties and methods of the Collection object, from the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type "Collection object" in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

↑ Back to the top


Keywords: KB198465, kbprogramming, kbhowto

↑ Back to the top

Article Info
Article ID : 198465
Revision : 5
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 321