PC Review


Reply
Thread Tools Rate Thread

Cannot iterate through the items in custom class collection (NewEnum())

 
 
Nando
Guest
Posts: n/a
 
      25th Jul 2008
Hi all! I have created two class modules: Book and Bookshelf. The last one
is implemented as a collection of items of the first one. I have included
the function NewEnum() in the Bookshelf class, so I can iterate through the
elements of the collections using a "For Each" loop. The problem is that
code below comes up with a runtime error '438': "Object doesn't support this
property or method." However this code does work under VB6. What's wrong?!
How can I make it to work under Access XP? The only way I can get it to work
is using the second code "FormV2_Load()"

'This version does not work
Private Sub Form_Load()

Dim MyBookshelf As Bookshelf
Dim Item As Book

Me.List1.RowSourceType = "Value List"

Set MyBookshelf = New Bookshelf

For Each Item In MyBookshelf
Me.List1.AddItem Item.Title & vbTab & Item.ISBN
Next Item

Set Item = Nothing
Set MyBookshelf = Nothing

End Sub

'This version does work
Private Sub FormV2_Load()

Dim MyBookshelf As Bookshelf
Dim Item As Book
Dim c As Long

Me.List1.RowSourceType = "Value List"

Set MyBookshelf = New Bookshelf

For c = 1 To MyBookshelf.Count
Set Item = MyBookshelf.Item(c)
Me.List1.AddItem Item.Title & vbTab & Item.Author & vbTab &
Item.ISBN
Next

Set Item = Nothing
Set MyBookshelf = Nothing

End Sub

Below is the code I included inside Bookshelf to make sure I can use the
"For Each .. Next". This is the collection object enumerator, so it can know
how to iterate through the items in the collection:

' NewEnum must return the IUnknown interface of a
' collection's enumerator.
Public Function NewEnum() As IUnknown
Set NewEnum = mcolEmployees.[_NewEnum]
End Function

I appreciate it your assistance. Thanks!


 
Reply With Quote
 
 
 
 
bcap
Guest
Posts: n/a
 
      25th Jul 2008
VBA is not VB6! This might help:

http://p2p.wrox.com/topic.asp?TOPIC_ID=26259

To do it in a more "VBA" kind of a way, have the Books collection as a
public member of the Bookshelf class.

"Nando" <(E-Mail Removed)> wrote in message
newsb9ik.261869$(E-Mail Removed)...
> Hi all! I have created two class modules: Book and Bookshelf. The last one
> is implemented as a collection of items of the first one. I have included
> the function NewEnum() in the Bookshelf class, so I can iterate through
> the elements of the collections using a "For Each" loop. The problem is
> that code below comes up with a runtime error '438': "Object doesn't
> support this property or method." However this code does work under VB6.
> What's wrong?! How can I make it to work under Access XP? The only way I
> can get it to work is using the second code "FormV2_Load()"
>
> 'This version does not work
> Private Sub Form_Load()
>
> Dim MyBookshelf As Bookshelf
> Dim Item As Book
>
> Me.List1.RowSourceType = "Value List"
>
> Set MyBookshelf = New Bookshelf
>
> For Each Item In MyBookshelf
> Me.List1.AddItem Item.Title & vbTab & Item.ISBN
> Next Item
>
> Set Item = Nothing
> Set MyBookshelf = Nothing
>
> End Sub
>
> 'This version does work
> Private Sub FormV2_Load()
>
> Dim MyBookshelf As Bookshelf
> Dim Item As Book
> Dim c As Long
>
> Me.List1.RowSourceType = "Value List"
>
> Set MyBookshelf = New Bookshelf
>
> For c = 1 To MyBookshelf.Count
> Set Item = MyBookshelf.Item(c)
> Me.List1.AddItem Item.Title & vbTab & Item.Author & vbTab &
> Item.ISBN
> Next
>
> Set Item = Nothing
> Set MyBookshelf = Nothing
>
> End Sub
>
> Below is the code I included inside Bookshelf to make sure I can use the
> "For Each .. Next". This is the collection object enumerator, so it can
> know how to iterate through the items in the collection:
>
> ' NewEnum must return the IUnknown interface of a
> ' collection's enumerator.
> Public Function NewEnum() As IUnknown
> Set NewEnum = mcolEmployees.[_NewEnum]
> End Function
>
> I appreciate it your assistance. Thanks!
>



 
Reply With Quote
 
Nando
Guest
Posts: n/a
 
      26th Jul 2008
Thanks bcap! That did it! I'm so happy now. This is a summary of the steps
for any one reading out here:

1) Create your class then export it as text.
2) Delete the class from the project.
3) Edit the text file to implement the procedure attributes, inserting the
line "Attribute NewEnum.VB_UserMemId = -4" right below the function header
"NewEnum()."
4) Re-import the class.

Also I have been playing around a lot and I found it is lot easier to just
use the Class Builder utility under VB6, and import the .CLS files into
Access. Cool stuff! Thanks bcap!

"bcap" wrote:
> VBA is not VB6! This might help:
>
> http://p2p.wrox.com/topic.asp?TOPIC_ID=26259
>
> To do it in a more "VBA" kind of a way, have the Books collection as a
> public member of the Bookshelf class.



 
Reply With Quote
 
Nando
Guest
Posts: n/a
 
      26th Jul 2008
"bcap" wrote:

> To do it in a more "VBA" kind of a way, have the Books collection as a
> public member of the Bookshelf class.


How will that look like? I cannot picture it and I'm curious. This is a
sample of the interaction through code:


Dim MyBookshelf As Bookshelf
Dim Item As Book

Me.List1.RowSourceType = "Value List"

Set MyBookshelf = New Bookshelf

For Each Item In MyBookshelf
Me.List1.AddItem Item.Title & vbTab & Item.ISBN
Next Item

Set Item = Nothing
Set MyBookshelf = Nothing


 
Reply With Quote
 
Nando
Guest
Posts: n/a
 
      26th Jul 2008
"Nando" wrote:

> Thanks bcap! That did it! I'm so happy now. This is a summary of the steps
> for any one reading out here:
>
> 1) Create your class then export it as text.
> 2) Delete the class from the project.
> 3) Edit the text file to implement the procedure attributes, inserting the
> line "Attribute NewEnum.VB_UserMemId = -4" right below the function header
> "NewEnum()."
> 4) Re-import the class.
>
> Also I have been playing around a lot and I found it is lot easier to just
> use the Class Builder utility under VB6, and import the .CLS files into
> Access. Cool stuff! Thanks bcap!


As a note: I just found out that if you cut and paste the procedures, during
code editing, these member attributes may be lost, so the importing will
have to be done again.


 
Reply With Quote
 
bcap
Guest
Posts: n/a
 
      26th Jul 2008
Dim MyBookshelf As Bookshelf
Dim Item As Book

Me.List1.RowSourceType = "Value List"

Set MyBookshelf = New Bookshelf

For Each Item In MyBookshelf.Books
Me.List1.AddItem Item.Title & vbTab & Item.ISBN
Next Item

Set Item = Nothing
Set MyBookshelf = Nothing


"Nando" <(E-Mail Removed)> wrote in message
news:Mruik.134926$(E-Mail Removed)...
> "bcap" wrote:
>
>> To do it in a more "VBA" kind of a way, have the Books collection as a
>> public member of the Bookshelf class.

>
> How will that look like? I cannot picture it and I'm curious. This is a
> sample of the interaction through code:
>
>
> Dim MyBookshelf As Bookshelf
> Dim Item As Book
>
> Me.List1.RowSourceType = "Value List"
>
> Set MyBookshelf = New Bookshelf
>
> For Each Item In MyBookshelf
> Me.List1.AddItem Item.Title & vbTab & Item.ISBN
> Next Item
>
> Set Item = Nothing
> Set MyBookshelf = Nothing
>
>



 
Reply With Quote
 
Nando
Guest
Posts: n/a
 
      26th Jul 2008
"bcap" wrote:

> "Nando" <(E-Mail Removed)> wrote in message
> news:Mruik.134926$(E-Mail Removed)...
>> "bcap" wrote:
>>
>>> To do it in a more "VBA" kind of a way, have the Books collection as a
>>> public member of the Bookshelf class.

>>
>> How will that look like? I cannot picture it and I'm curious.


OK, following your recommendation I had to redesign my object oriented
model, so item iteraction makes more sense. I learned a lot on the way and
hopefully I got it right now.

Book 'Class
Title As String
Author As String
ISBN As String

Books 'Class/Collection
Item As Book
Count As Long
Add()
Remove()
NewEnum()

Bookshelf 'Class
Tag As String
Location As String
Books As Books

'Base on this Object Model I was playing to do this iteraction: (Great it
works!)

Private Sub Form_Load()

Dim MyBookshelf As Bookshelf
Dim Item As Book

Set MyBookshelf = New Bookshelf

With MyBookshelf

'add some test
.Tag = "A-D"
.Location = "South window"

.Books.Add "Learn VB in 5 minutes!", "LL. Liar", "1111111111"
.Books.Add "Mastering Access 2009", "MS Press", "2222222222"
.Books.Add "How to buy the best PC", "M. Dell", "3333333333"

'Print the books
For Each Item In MyBookshelf.Books
Debug.Print Item.Title & vbTab & Item.Author & vbTab & Item.ISBN
Next Item
Set Item = Nothing

End With

Set MyBookshelf = Nothing

End Sub


 
Reply With Quote
 
bcap
Guest
Posts: n/a
 
      27th Jul 2008
Terrific, looks good to me!

"Nando" <(E-Mail Removed)> wrote in message
news:XIMik.266679$(E-Mail Removed)...
> "bcap" wrote:
>
>> "Nando" <(E-Mail Removed)> wrote in message
>> news:Mruik.134926$(E-Mail Removed)...
>>> "bcap" wrote:
>>>
>>>> To do it in a more "VBA" kind of a way, have the Books collection as a
>>>> public member of the Bookshelf class.
>>>
>>> How will that look like? I cannot picture it and I'm curious.

>
> OK, following your recommendation I had to redesign my object oriented
> model, so item iteraction makes more sense. I learned a lot on the way and
> hopefully I got it right now.
>
> Book 'Class
> Title As String
> Author As String
> ISBN As String
>
> Books 'Class/Collection
> Item As Book
> Count As Long
> Add()
> Remove()
> NewEnum()
>
> Bookshelf 'Class
> Tag As String
> Location As String
> Books As Books
>
> 'Base on this Object Model I was playing to do this iteraction: (Great it
> works!)
>
> Private Sub Form_Load()
>
> Dim MyBookshelf As Bookshelf
> Dim Item As Book
>
> Set MyBookshelf = New Bookshelf
>
> With MyBookshelf
>
> 'add some test
> .Tag = "A-D"
> .Location = "South window"
>
> .Books.Add "Learn VB in 5 minutes!", "LL. Liar", "1111111111"
> .Books.Add "Mastering Access 2009", "MS Press", "2222222222"
> .Books.Add "How to buy the best PC", "M. Dell", "3333333333"
>
> 'Print the books
> For Each Item In MyBookshelf.Books
> Debug.Print Item.Title & vbTab & Item.Author & vbTab &
> Item.ISBN
> Next Item
> Set Item = Nothing
>
> End With
>
> Set MyBookshelf = Nothing
>
> End Sub
>
>



 
Reply With Quote
 
Nando
Guest
Posts: n/a
 
      27th Jul 2008
"bcap" wrote:
> Terrific, looks good to me!
>
>>>>> To do it in a more "VBA" kind of a way, have the Books collection as
>>>>> a public member of the Bookshelf class.
>>>>


Thanks a million!


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a collection of custom classes in another custom class kagard Microsoft Access 3 12th Jun 2011 07:57 PM
Class Collection Add Items keep repeating =?Utf-8?B?S2V2aW4gVmF1Z2hu?= Microsoft Excel Programming 3 18th Apr 2006 10:50 PM
Can't get collection to save when using collection of custom class as property of control in VS 2005 J.Edwards Microsoft Dot NET Compact Framework 0 10th Jan 2006 04:44 AM
Want function or class that uses reflection to iterate over properties in custom object Microsoft C# .NET 2 9th Dec 2005 11:27 AM
Collection in a custom class Rick Palmer Microsoft VB .NET 2 31st Oct 2003 08:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:26 PM.