Solved

add 1 row of a recordset to a collection

Posted on 2014-02-13
7
308 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 51

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 38

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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 38

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

631 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