Solved

Collections

Posted on 2000-03-06
7
343 Views
Last Modified: 2010-05-02
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














0
Comment
Question by:jjjjjjj
7 Comments
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2588965
Can you paste the exact code here that didn't work?

Wes
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2590192
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


0
 
LVL 4

Expert Comment

by:TigerZhao
ID: 2594949
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jjjjjjj
ID: 2597702
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
0
 
LVL 1

Author Comment

by:jjjjjjj
ID: 2597703
This question has a deletion request Pending
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2597889
This question no longer is pending deletion
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
ID: 2597890
from my earlier comment.. << you did not create a collection object before hand.. and that in the second case (the procedure).. you probably did >>

Scope is everything.. <smile>

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question