In the below loop I add a Class Object to a collection within another class object, which itself is in a collection.
Dim opportunity As New ClmOpportunity
opportunity.name = name
owners.item(overallOwner).addOpportunity opportunity
MsgBox opportunity.name
Next i
MsgBox owners("John Smith").opportunities(1).name
The first message box presents the correct opportunity name, but the second message box gets set to the last opportunity that was added, even though John Smith is first in the collection.
So if I have two owners, John Smith with opportunity 1 and Mary Lou with opportunity 2 the out put from the second message box will be opportunity 2 for both records.
But the first message will be opportunity 1 and 2 as expected.
This is the code from the Owner class module:
Public name As Variant
Public opportunities As New collection
Public Function addOpportunity(opp As ClmOpportunity)
Dim OppID As String
OppID = opportunities.count + 1
opp.ID = OppID
opportunities.Add opp, OppID
End Function
So the solution to this was to instantiate the opportunity outside of the loop then reinitialise each time like this: