Solved

add 1 row of a recordset to a collection

Posted on 2014-02-13
7
303 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 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 36

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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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 36

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataType for bit Comparison 11 47
Linking Master Child Fields 2 24
MS Access, hourly employee status 8 40
access query - filter field for particular number format 3 26
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

685 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