Solved

add 1 row of a recordset to a collection

Posted on 2014-02-13
7
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 50

Assisted Solution

by:Gustav Brock
Gustav Brock earned 400 total points
ID: 39858312
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 37

Assisted Solution

by:PatHartman
PatHartman earned 100 total points
ID: 39858903
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
ID: 39859335
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
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 5

Author Comment

by:rberke
ID: 39859374
"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 37

Expert Comment

by:PatHartman
ID: 39859390
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
ID: 39861811
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
ID: 39872901
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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