Cannot reference object in custom collection

D

Dave

This one has be stumped. I loop through a collection of form textboxes
and add the control name and value as two properties of a class
object, then add the object to a collection. At any point while
stepping through, I can debug.print the two properties and check that
the collection count has incremented by 1. All would seem well.
However, if I reference the collection item in the immediate window
while stepping, the reference always returns the properties for the
last added item. For example, when the collection count is 6 and I ask
for the count [?colvariable.count] I get 6 . If I ask for the name and
value for item 2 [?colVariable.item(2).ctlname & "..." &
colvariable.Item(2).qty]
I get txtQty6... [no value because there isn't one]

If I do this when the first item holding a quantity has been added, I
get txtQty2...5, which is fine. It seems that whenever I check for
colVariable.item(n), it will always return the last item added to the
collection even though I've specified a key and it is different each
time.
It's probably something simple (sigh)...

Class Code (explicit option not shown) in clsVariable
Private svctlName As String
Private varQty As Variant

Public Property Get ctlName() As String
ctlName = svctlName
End Property
Public Property Let ctlName(value As String)
svctlName = value
End Property
Public Property Get Qty() As Variant
Qty = varQty
End Property
Public Property Let Qty(value As Variant)
varQty = value
End Property
*****
declaration section of form module:
Dim myClass As New clsVariable
Dim colVariable As New Collection
****
form listbox click event:
some other dim's
Dim ctl As Control
more code, then the loop
For Each ctl In Forms!frmJoblist!subWorkDteDtl.Form.Controls
ifs
end ifs
ctlAdd ctl.Name, ctl.value 'call the function to add the item
Next

Public Function ctlAdd(svctlName As String, varQty As Variant)
Dim svKey As String
With myClass
.ctlName = svctlName
.Qty = varQty
svKey = svctlName
End With
colVariable.add Item:=myClass, Key:=svKey
If myClass.Qty <> "" Then Debug.Print myClass.ctlName & " " &
myClass.Qty
End Function

Anyone who can solve this will be my hero!
Thanks in advance.
 
D

Dave

This one has be stumped. I loop through a collection of form textboxes
and add the control name and value as two properties of a class
object, then add the object to a collection. At any point while
stepping through, I can debug.print the two properties and check that
the collection count has incremented by 1. All would seem well.
However, if I reference the collection item in the immediate window
while stepping, the reference always returns the properties for the
last added item. For example, when the collection count is 6 and I ask
for the count [?colvariable.count] I get 6 . If I ask for the name and
value for item 2 [?colVariable.item(2).ctlname & "..." &
colvariable.Item(2).qty]
I get txtQty6... [no value because there isn't one]

If I do this when the first item holding a quantity has been added, I
get txtQty2...5, which is fine. It seems that whenever I check for
colVariable.item(n), it will always return the last item added to the
collection even though I've specified a key and it is different each
time.
It's probably something simple (sigh)...

Class Code (explicit option not shown) in clsVariable
Private svctlName As String
Private varQty As Variant

Public Property Get ctlName() As String
ctlName = svctlName
End Property
Public Property Let ctlName(value As String)
svctlName = value
End Property
Public Property Get Qty() As Variant
Qty = varQty
End Property
Public Property Let Qty(value As Variant)
varQty = value
End Property
*****
declaration section of form module:
Dim myClass As New clsVariable
Dim colVariable As New Collection
****
form listbox click event:
some other dim's
Dim ctl As Control
more code, then the loop
For Each ctl In Forms!frmJoblist!subWorkDteDtl.Form.Controls
    ifs
    end ifs
    ctlAdd ctl.Name, ctl.value 'call the function to add the item
Next

Public Function ctlAdd(svctlName As String, varQty As Variant)
Dim svKey As String
With myClass
    .ctlName = svctlName
    .Qty = varQty
    svKey = svctlName
End With
colVariable.add Item:=myClass, Key:=svKey
If myClass.Qty <> "" Then Debug.Print myClass.ctlName & "   " &
myClass.Qty
End Function

Anyone who can solve this will be my hero!
Thanks in advance.

Sorry, the hero job is taken. The object was created outside the loop.
I moved it and it works fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top