Solved

Reuse Recordset?

Posted on 2004-10-08
5
878 Views
Last Modified: 2012-06-27
I'm wondering if there is any reason why I should not reset a recordset and query definition rather than create a new one.  See code snippet for what I'm talking about.

----------------------------------
Code Snippet
----------------------------------

Dim db As DAO.Database
Dim qAppt As DAO.QueryDef
Dim rAppt As DAO.Recordset
Dim pAppt As DAO.Parameters

Set db = CurrentDb()
Set qAppt = db.QueryDefs("qfrmAppts_Validate") 'set query definition for the first time
qAppt.Parameters("vDate") = Me!fldInsertDate
Set rAppt = qAppt.OpenRecordset(dbOpenDynaset) 'set recordset for the first time

If Not rAppt.EOF Then
    Do some stuff.........
End If

Set qAppt = db.QueryDefs("qryAppts_Insert") 'reset query definition
Set rAppt = qAppt.OpenRecordset(dbOpenDynaset) 'reset recordset

While Not rAppt.EOF
    Do some stuff.........
Wend


Thanks!

Michelle
0
Comment
Question by:mixxie
5 Comments
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12261949
Not really, so long as you remember to close and set the objects to Nothing in order to release the memory that they have taken up. I often reuse recordset objects (in particular) in the same piece of code.
0
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 200 total points
ID: 12261957
The main reason you might want to declare a different object for each recordset that you want to use is really code clarity. It can be confusing if you are trying to determine which source table/query the recordset is referencing at a particular time.
0
 
LVL 8

Assisted Solution

by:jb79uk
jb79uk earned 150 total points
ID: 12261962
Hi Michelle,

As a matter of good practice, I'd recommend Cosing the Recordset object before you attempt to reuse it. It might also be a good idea to explicitly set the QueryDef to Nothing before reusing it.

Hope something here helps

JB
0
 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 150 total points
ID: 12263325
Just had to chime in...same thing, different expert.

I normally do NOT reuse objects, but that's because I do not want to come back to a project 6 months from now and wonder "why the $%#@ am I using query 1's recordset to generate query 2's report?!?"  Do not dismiss the affect clear and concise coding has on a project.

Other than that, sure thing!  Reusability is exactly why object-oriented programming was invented in the first place.  JB's suggestion is a good one...clean up after yourself before reusing, and I would add a comment notating the change in responsibilities for the object as well.
0
 

Author Comment

by:mixxie
ID: 12275947
Thanks, guys!  I appreciate the quick responses!

Michelle
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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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 …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

770 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