Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA - How to add a collection to a collection of collections

I am trying to create code to represent a form document using VBA in Word 2007. I have created classes to represent Section, QuestionSet and Question.

So I have 15 Sections. I have created a function to create each 'Section' Object add it to the 'Sections' Collection then destroy the object, the result being that the objects remain persistent in the collection (or something).

Is it possible to use the same method to add collections to collections or would I have to define each collection explictly?

Code in Module:

Public Sections As Collection

Function DefineSection(ByVal SectionName As String)

    Set Section = New clsSection
    Section.myName = SectionName
    Sections.Add Section, SectionName

End Function


Function DefineQuestionSet(ByVal SectionName As String, ByVal Name As String, ByVal NoOfQuestions As Integer, ByVal IsMutuallyExclusive As Boolean, Optional ByVal DependentOnSection As String)

    Dim Qsets As Collection

    Set Qsets = New Collection
    Set QuestionSet = New clsQuestionSet

    QuestionSet.Name = Name
    QuestionSet.NoOfQuestions = NoOfQuestions
    QuestionSet.MutuallyExclusive = IsMutuallyExclusive

    If Not (DependentOnSection) = "" Then
        QuestionSet.DependentOnSection = DependentOnSection
    End If

    Qsets.Add QuestionSet
    Sections.Item(SectionName).Add Qsets

End Function

Then this is called via:

Sub Initilise()

    Set Sections = New Collection

    DefineSection "PersonalDetails"
    DefineQuestionSet "PersonalDetails", "PersonalDetails", 29, False

End Sub
like image 554
Stevo Avatar asked Feb 25 '11 11:02

Stevo


People also ask

What is the difference between array and collection in VBA?

Collections in VBA are objects that can store groups of related items, much like an array. Unlike arrays, a single collection can store items of different types because each item in a collection is stored as a Variant.

How do I create a dynamic array in VBA?

Create a Dynamic Array in VBAFirst, declare an array with its name. After that, the elements count left the parentheses empty. Now, use the ReDim statement. In the end, specify the count of elements you want to add to the array.

Is range a collection VBA?

Collections are a very important part of VBA. If you have used the language for any length of time then you will have used Collections. The most common ones are the Workbooks, Worksheets, Range and Cells collections.


2 Answers

Try this simple example:

Private Sub CommandButton1_Click()

    Dim masterCollection As New collection
    masterCollection.Add "first key", createDetailCollection()
    masterCollection.Add "second key", createDetailCollection()
    masterCollection.Add "third key", createDetailCollection()

End Sub

The follow function return a initializated collection

Function createDetailCollection()
    Dim collection As New collection
    createCollection = collezioneBuy
End Function
like image 21
daniele3004 Avatar answered Oct 14 '22 21:10

daniele3004


Yes. You can absolutely add collections to collections to collections ad infinitum. The code you have posted looks like it should work just from glancing through it. Are you having specific problems?

UPDATE: VBA only passes around references to objects. If you explicitly destroy an object after assigning it to a collection (eg, Set myObj = Nothing) then you will also be destroying the object inside the collection.

[EDIT]: Apparently this is not true. From this website (first linked by Stevo in the comments):

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.

UPDATE: There's no reason why you can't add a collection to an object. You just need the class your object is instantiated from to support such a method. For example, in your clsSection class module you need an Add method which adds objects passed to it to a collection stored in the clsSection:

Private QSetsColl As Collection

Public Sub Add(QSets As Object)
    If QSetsColl Is Nothing Then Set QSetsColl = New Collection
    QSetsColl.Add QSets
End Sub
like image 70
mwolfe02 Avatar answered Oct 14 '22 22:10

mwolfe02