jjjjjjj
asked on
Collections
Why can't a programmer add more than one occurance of an object to a collection in the same procedure, ie.. while processing result sets, Vb6 would not add the correct result set field data to the collection in the same procedure. I had to create an add sub procedure and pass it the record in order to have it added to the collection properly. Listed below is an example:
This did not work properly:
While RDORS not EOF
move RSFields to object fields
Add to collection
RS.Movenext
Wend
This did work:
While RDORS not EOF
Call subprocedure passing RDORS object
(subprocedure add to collection)
RS.Movenext
Wend
TIA,
jjjjjjj
This did not work properly:
While RDORS not EOF
move RSFields to object fields
Add to collection
RS.Movenext
Wend
This did work:
While RDORS not EOF
Call subprocedure passing RDORS object
(subprocedure add to collection)
RS.Movenext
Wend
TIA,
jjjjjjj
What it looks like to me, is that in the first case (the one that didn't work).. you did not create a collection object before hand.. and that in the second case (the procedure).. you probably did.
Try this code out (I am using a Listview collection as an example)
Dim itmWork as Listitem
While RDORS not EOF
' Create collection object
Set itmWork = lvwListview.ListItems.Add( )
' Move fields
With itmWork
.Key = Rs.Key
.SmallIcon = NameOfPicture
.Text = Rs.Text
.subitems(1) = Rs.Field1
.subitems(2) = Rs.Field2
.subitems(3) = Rs.Field3
End With
' Get Next Record
RS.Movenext
Wend
Try this code out (I am using a Listview collection as an example)
Dim itmWork as Listitem
While RDORS not EOF
' Create collection object
Set itmWork = lvwListview.ListItems.Add(
' Move fields
With itmWork
.Key = Rs.Key
.SmallIcon = NameOfPicture
.Text = Rs.Text
.subitems(1) = Rs.Field1
.subitems(2) = Rs.Field2
.subitems(3) = Rs.Field3
End With
' Get Next Record
RS.Movenext
Wend
follow code did not work
Dim o as New Collection
'...
o.Add rs!Field1
'...
rs.Close
'...
debug.print o(I)
Because you add the Field OBJECT to the collection, after recordset close, the field object cann't access, the right way is:
o.Add rs!Field1.Value
Dim o as New Collection
'...
o.Add rs!Field1
'...
rs.Close
'...
debug.print o(I)
Because you add the Field OBJECT to the collection, after recordset close, the field object cann't access, the right way is:
o.Add rs!Field1.Value
ASKER
I am deleting because I have solved the issue separately, without using any of the comments.
The reason why it does not work is due to scope. I have resolved by re-establishing the proper scope.
jjjjjjj
The reason why it does not work is due to scope. I have resolved by re-establishing the proper scope.
jjjjjjj
ASKER
This question has a deletion request Pending
This question no longer is pending deletion
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Wes