Steven
Steven

Reputation: 781

VBA Passing Object into another Objects Collection

I have a set of owners, who each have their own set of opportunities.

I have two class modules, ClmOpportunity which has a bunch of properties, and ClmOwner which has a single name property and a Collection storing ClmOpportunity Objects:

Public name As Variant
Private opps As New collection

Public Function addOpportunity(opp As ClmOpportunity)

    opp.ID = opps.Count + 1
    opps.Add opp, opps.Count + 1

End Function

These owner objects are also being stored in a collection in my main module. When I try to use the function addOpportunity as shown below:

Dim item As New ClmOpportunity

item.name = "test"

owners.item(overallOwner).addOpportunity (item)

I get the error:

"object doesn't support this property or method"

I am quite new to VBA and I don't understand why this is, I am passing in a ClmOpportunity, so it should be fine right?

Any help would be greatly appreciated!

Upvotes: 1

Views: 2046

Answers (1)

Tim Williams
Tim Williams

Reputation: 166835

You don't use parentheses if there's no return value...

owners.item(overallOwner).addOpportunity item

...then you'll get a "type mismatch" error because a collection expects a string value as a key, so you'll need to adjust your addOpportunity function (which should probably be a Sub if you don't intend adding a returned value)

Upvotes: 6

Related Questions