Solved

add 1 row of a recordset to a collection

Posted on 2014-02-13
7
300 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
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 35

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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 35

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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