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 :/