Solved

Reuse Recordset?

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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…

830 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