473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Useful refactoring/function for DAO object clean-up

Hi all,

I had just participating in a small thread here about when to set recordset
variables to Nothing when I ran into an interesting case in my own code that
highlighted the need for a formal, centralized function to deal with this.

The case that came up was that I have a class module that handles 2 recordsets
that it must clean up at termination, but the termination should not assume
that the recordsets have necessarily been opened. here's the initial code I
wrote for this...

Private Sub Class_Terminate()
If Not (mrstSchemaFields Is Nothing) Then
mrstSchemaFields.Close
Set mrstSchemaFields = Nothing
End If
If Not (mrstSchemaTables Is Nothing) Then
mrstSchemaTables.Close
Set mrstSchemaTables = Nothing
End If
End Sub

Note that the database object belongs to the calling code and is not cleaned
up in the class.
This is an obvious cut-and-paste duplication maintenance problem. It would be
easy to make a change, and end up referencing the wrong recordset in the wrong
place, and have the code not do what it should. Furthermore, the code might
not fail with an error, and the problem would only be detected later when
Access might fail to completely close. In an audit of the code, the problem
would be very obscure and hard to track down. In fact, I once spent several
days tracking sown a similar problem by stopping the code at various places,
then trying to close Access, checking the Windows Task Manager, etc. until I
finally got down to the individual function that caused the problem.

By taking advantage of VB's reference parameters, though, it's possible to
create a central procedure to eliminate this issue. For a single recordset
reference, the procedure does the job of checking for Nothing, closing the
recordset, and setting the variable to Nothing after closing it. It might be
overkill in situations other than the above, but removing duplication by using
this same call everywhere means that anything else that you ever want to
always do when closing a recordset can be added in a single place at any time.

' This procedure closes the recordset if it is open, and sets the
' recordset variable to Nothing to allow the closed recordset object
' to go out of scope immediately. Since the variable is passed by
' reference, setting the parameter to Nothing operates on the variable
' passed from the calling function.
Public Sub DAOCloseAndReleaseRecordset(ByRef rst As DAO.Recordset)
If rst Is Nothing Then Exit Sub ' No object referenced, so nothing to do.
On Error Resume Next ' Don't raise error if recordset already closed.
rst.Close
Set rst = Nothing
End Sub
Here's the new version of the original code, now employing the new function.

Private Sub Class_Terminate()
DAOCloseAndReleaseRecordset mrstSchemaFields
DAOCloseAndReleaseRecordset mrstSchemaTables
End Sub
Nov 12 '05 #1
0 1446

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chinook | last post by:
OO refactoring trial ==================== Following is a simple trial structure of a refactoring (top-down to OO) learning exercise I'm doing. Whether you call it a Factory pattern, COR pattern,...
0
by: Andre Baresel | last post by:
Hello together, just a year ago I was searching arround for a tool supporting refactoring for c++. I've seen implementations for java and was impressed how an IDE can help with such a feature....
7
by: eyh5 | last post by:
Hi, I'm writing some C codes to run simulations. I'm wondering if there is a website that may contain useful information on how to make one's code run more efficiently and in a...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
8
by: Frank Rizzo | last post by:
I keep hearing this term thrown around. What does it mean in the context of code? Can someone provide a definition and example using concrete code? Thanks.
3
by: Francesc | last post by:
Hi, After some years programming libraries and web based applications I'm trying to program my first not-silly windows application (something similar to an editor but just to annotate text...
15
by: Simon Cooke | last post by:
Does anyone know of any tools for refactoring header files? We're using a third party codebase at work, and pretty much every file includes a 50Mb precompiled header file. I'm looking for a tool...
7
by: Kamilche | last post by:
''' I'm in the middle of a refactoring dilemma. I have several singletons that I'm turning into modules, for ease of access. The usual method is noted as 'Module 1' below. The new method is...
6
by: John Salerno | last post by:
My code is below. For now I'm focusing on the lines where health (and armor) are increased in each character class. Let's say I decided to change the amount of increase in the future. As it is now,...
2
by: pingu219 | last post by:
Hi I'm currently in the midst of building a C high-level refactoring program in Java but I was wondering if there are any good parsers (or some other alternative) which are able to read in C files...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.