Am new to VBA object orientated stuff and have hit an early hurdle.

I've created a collection of boat objects with an array of boats and an integer counter. Storing the boats isn't a problem however when I try to retrieve the boats I'm getting Object variable with block '91' not set issues. For e.g.

In Main I'm calling

Dim Boats as New BoatCollection

...Code to insert boats is ok

'Now get a boat

Dim aBoat as Boat

Set aBoat = Boats.GetBoat(0)

The GetBoat code is;

Public Function GetBoat(IndexIn as Integer) as Boat

GetBoat = BoatArray(IndexIn)

End Function

The GetBoat = BoatArray(IndexIn) is raising the exception.

Any help appreciated.

P.S. Any pointers on passing values to the object upon creation would be nice too.



Re: Returning a custom object from a custom collection


VBA is off topic for this forum, this forum deals with VB.NET questions

For VBA Questions use the VBA Forum

http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=74&SiteID=1

That said, I'll point you to what happening.

You have instantiated a collection called boats. You have not added any objects to the collection so it is empty.

Code Snippet

Dim Boats as New BoatCollection

Then you try and get a boat object from the collection - your trying to get the 1st item in the collection using the following two lines

Code Snippet

Dim aboat as boat

set aboat = Boats.getBoat(0)

However there is no first item in the collection as you never added any boat to the collection.

Add a record to the collection this will work, in you code verify the collection has items in by checking the count property.

That said, VBA Questions are off topic for this forum. Hope that points you in the right direction.

Re: Returning a custom object from a custom collection


Spotty, point taken will take the post to there now, thanks for the reply however the collection is successfully filled and index 0 is valid.



Re: Returning a custom object from a custom collection

Dick Donny

Hi Mike

I think you missed a SET statement in your function

SET GetBoat = BoatArray(IndexIn)