Solved

Reuse Recordset?

Posted on 2004-10-08
5
872 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
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…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now