Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 16th, 2011, 07:53 AM
Registered User
 
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Post Creating a sub to close recordsets elsewhere in the database

Hello,


I am aware that in Access VBA, it is important to close any objects that you open, like DAO databases, recordsets, etc.

What I wanted to know is, are their any implications of creating a sub and calling this everytime I wanted to close a recordset. For example:


================================================== =====
Public Sub DoSomething()

Dim dbs As DAO.Database
Dim rst As DAO.Recordset

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("SELECT * FROM ATable;")

' etc
' etc
' etc

Call subCloseObject(, rst)
Call subCloseObject(dbs)

End Sub
================================================== =====
Public Sub subCloseObject(Optional ByRef dbs As DAO.Database, Optional ByRef rst As DAO.Recordset)

10 On Error GoTo ErrorCode

100 If Not dbs Is Nothing Then
110 Call dbs.Close
120 Set dbs = Nothing
130 End If

200 If Not rst Is Nothing Then
210 Call rst.Close
220 Set rst = Nothing
230 End If

60000 ExitCode:
60010 Call DoCmd.SetWarnings(True)
60020 Exit Sub

65000 ErrorCode:
65010 Call subErrorNotifier(Err.Number, "{1AC8A26B-57BB-495C-BE83-025664F55551}", Erl)
65020 Resume ExitCode
================================================== =====

Since I have stated ByRef I don't think there are any issues other than this close procedure creating a "Nothing" database/recordset if none is stated. However, if an object is "Nothing" then do I need to worry about it?

Still learning VBA so any help would be greatly appreciated, many thanks.





Similar Threads
Thread Thread Starter Forum Replies Last Post
update database when browsr close artarasan Classic ASP Professional 7 March 23rd, 2009 07:51 PM
When should you close the database connection? Stickannn Pro PHP 0 September 13th, 2007 04:25 AM
Close Database using VBA Stanny Access VBA 4 May 31st, 2006 01:49 AM
Creating/Populating Recordsets in ADO dgulliver VB How-To 3 May 24th, 2006 03:31 AM
How to close database opened by adodc aa_azadeh Pro VB Databases 1 September 28th, 2004 08:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.