Solved

add 1 row of a recordset to a collection

Posted on 2014-02-13
7
298 Views
Last Modified: 2014-02-20
I have a recordset with 25 rows.  I want to collect the 5th and 6th rows then deallocate the recordset.

A hard way is shown below.

I wonder if there is an easier way? BTW, I am not committed to using a Collection. For instance perhaps I can create a class object to help? But, I do need to be able to reference the elements by column name (e.g   !mName) so putting the elements into a 2 dimensional array would not be useful.

Anything easier would be appreciated.
Sub t639()
Dim rs As DAO.Recordset
Dim var As Variant
Dim oneRec As Collection
Dim allRecs As New Collection


sql = "select * from tblmain where maccttype in ('Direct') and  mname like 's*'"

Set rs = CurrentDb.OpenRecordset(sql)
i = 0
Do Until rs.EOF
i = i + 1
Debug.Print rs!mname
If i = 5 Or i = 6 Then
    Set oneRec = New Collection
    For Each var In rs.Fields
        oneRec.Add var.value, var.name
    Next
    allRecs.Add oneRec
    
End If
rs.MoveNext
Loop

For Each var In allRecs
    MsgBox var("mname")
Next
End Sub

Open in new window

0
Comment
Question by:rberke
  • 4
  • 2
7 Comments
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 400 total points
Comment Utility
Nothing wrong with a collection, but - if this is going to make sense - you need to let your recordset be ordered by one or more fields.

/gustav
0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 100 total points
Comment Utility
What is different about the 5th and 6th rows that makes you want to collect them and why can't you process them as you loop through the recordset?  Creating an array or collection or whatever and then looping through that just obfuscates the process and adds overhead.  The best solution is to always use the criteria in the query to return just the records you want.

Row order is meaningless in a recordset unless you specifically order it and even then picking some arbitrary row doesn't make sense.  Sometimes people want the first or the last but other rows?  What happens if the recordset has only 3 rows?
0
 
LVL 5

Author Comment

by:rberke
Comment Utility
Maybe a dozen times is the last few years, I have had to resort to using code lines like my lines 16 to 20 "for each field in recSet.Fields ......".  

I have always felt that this code was cumbersome and inefficient. I decided to post a simple example and see if anyone had other approaches.  

So, to answer your question: Its just and example -- the only reason I am saving the 5th and 6th record is to demonstrate the issue.  

The real life business problem that hit me today, had a much more complicated structure which I have already solved and is beyond the scope of this question.

So, don't worry too much about my question. If you don't have a "better" solution, just ignore this question.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Author Comment

by:rberke
Comment Utility
"Creating an array or collection or whatever and then looping through that just obfuscates the process and adds overhead".

Sometimes that is true, but in really large programs it is often better to isolate functions.
For instance rather than have one subroutine with a 1000 lines of code, I might prefer 2.

set myCollection = FindProblemRecords(Recordset) ' 300 lines of code

call DealWithProblems(myCollection).   ' 700 lines of code.

I try to limit my subroutines to 100 or 200 lines of code.  Once they start getting over 500 lines I find they become very hard to maintain unless I break them into smaller chunks
0
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
The number of rows in a recordset doesn't impact the number of rows in a procedure.  Your procedure operates on a set of records.  Use the query to obtain the set rather than looping through a larger recordset and using some logic to whittle it down so you can save it somewhere and loop through it again.

Maybe you just choose a poor example but selecting items by row number is really not a sound solution.
0
 
LVL 5

Accepted Solution

by:
rberke earned 0 total points
Comment Utility
Thanks for trying, but this is going nowhere and it is too minor to be worth further efforts.  I am giving you points for your efforts, and closing the problem.
0
 
LVL 5

Author Closing Comment

by:rberke
Comment Utility
It is a minor problem, and I phrased the question poorly. Since it would take great effort for me to phrase it properly, I just want to close it.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

11 Experts available now in Live!

Get 1:1 Help Now