Results 1 to 2 of 2
  1. #1
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875

    Cannot Iterate Through my Collection of User-Defined Objects!

    OK, this is essentially a continuation of Building My Own Collection(s) - Attempt #2.

    I have my Class and now am trying to place it inside a Collection. The problem is, that when I try and iterate through my new collection, I get Runtime Error 438 - Object doesn't support this property of method.

    My Class Module: tSortItem
    Code:
    Option Compare Database
    Option Explicit
    
    Public Enum tSortType
      tSortAsc = 1
      tSortDesc = 2
      [_First] = 1
      [_Last] = 2
    End Enum
    
    Private tIndex As Long
    Private tField As String
    Private tLastUpdated As Date
    Private tTable As String
    Private tSort As tSortType
    
    ' Index is read/writeable
    Property Get Index() As Long
      Index = tIndex
    End Property
    
    Property Let Index(Value As Long)
      tIndex = Value
      tLastUpdated = Now()
    End Property
    
    
    ' Field is read/writeable
    Property Get Field() As String
      Field = tField
    End Property
    
    Property Let Field(Value As String)
      tField = Value
    End Property
    
    
    ' LastUpdated is read-only
    Property Get LastUpdated() As Date
      LastUpdated = tLastUpdated
    End Property
    
    
    ' Sort is read/writeable
    Property Get SortMethod() As tSortType
      SortMethod = tSort
    End Property
    
    Property Let SortMethod(Value As tSortType)
      tSort = Value
    End Property
    
    
    ' Table is read/writeable
    Property Get Table() As String
      Table = tTable
    End Property
    
    Property Let Table(Value As String)
      tTable = Value
    End Property
    My Collection Module: tSortList
    Code:
    Option Compare Database
    Option Explicit
    
    Private tCount As Long
    Private tItem As tSortItem
    
    Private tList As Collection
    
    Property Get Count() As Long
      Count = tCount
    End Property
    
    Property Let Count(Value As Long)
      tCount = Value
    End Property
    
    Public Sub Class_Initialize()
      Set tList = New Collection
    End Sub
    
    Public Sub AddItem(Table As String, Field As String, Optional SortMethod = tSortAsc)
      Dim tItem As tSortItem
    
      Dim nbrSortMethod As tSortType
    
      Set tItem = New tSortItem
    
      If SortMethod = 0 Then
        nbrSortMethod = tSortAsc
      Else
        nbrSortMethod = SortMethod
      End If
    
      ' 0 is the default value for an "empty" argument of type Long
      If SortMethod = 0 Then ' tSortType is basically a Long with attitude :)
        nbrSortMethod = tSortAsc
      Else
        nbrSortMethod = SortMethod
      End If
    
      With tItem
        .Table = Table
        .Field = Field
        .SortMethod = nbrSortMethod
        .Index = Me.Count
      End With
    
      tList.Add tItem
    
      Me.Count = Me.Count + 1
    End Sub
    My test code


    Code:
    Sub testrun()
      Dim tList As tSortList
      Dim tItem As tSortItem
    
      Set tList = New tSortList
    
      tList.AddItem "MyTable1", "MyField1", tSortAsc ' Test with explicit "Ascending" Sort
      tList.AddItem "MyTable2", "MyField2", tSortDesc ' Test with explicit "Descending" Sort
      tList.AddItem "MyTable3", "MyField3" ' Test with implicit "Ascending" Sort
    
      For Each tItem In tList
        MsgBox "Current tItem Object = " & tItem.Index
      Next tItem
    End Sub
    The red line in my test code is where I get the error. And by stepping through my code, I've been able to determine what's happening: For some reason, I'm creating a collection inside my collection (collectionception!) and all my class objects are being inserted into that (inner) collection instead of the the one I want them to do into!

    Anyone know what I'm doing wrong? I'm 90% sure it's in the structure of my Collection definition, but I have no idea where :/

  2. #2
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    I figured it out. Turns out you have to export the VBA Module to a text file, add the following code, and then importing it back in:
    Code:
    Public Function NewEnum() As IUnknown
    Attribute NewEnum.VB_UserMemId = -4
      Set NewEnum = tList.[_NewEnum]
    End Function
    If you want, you can enter the Function in the Microsoft Visual Basic Editor, but you'll still need to export it to add the line in blue.

    Apparently, by default, you can't iterate through user-defined collections and this workaround is the only way to fix it.

    Way to go MS, way to go...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 12-14-2012, 12:32 AM
  2. Replies: 4
    Last Post: 06-08-2012, 09:08 AM
  3. Replies: 2
    Last Post: 09-29-2011, 12:50 PM
  4. How to iterate through a custom collection
    By vicrauch in forum Programming
    Replies: 6
    Last Post: 07-21-2011, 02:51 PM
  5. Error: "User-defined type not defined"
    By mastromb in forum Programming
    Replies: 10
    Last Post: 01-08-2010, 02:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums