Solved

Collections

Posted on 2000-03-06
7
330 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

816 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now