Two questions: What happens at the line Set DB = Nothing? I thought it clears memory or something but I am uncertain. What happens if you exit the procedure because of an error before you get to this line? Is something left undone?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Set DB = Nothing (2003 sp2)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Set DB = Nothing (2003 sp2)
- This topic has 4 replies, 3 voices, and was last updated 17 years, 2 months ago.
AuthorTopicWSchuckrau
AskWoody LoungerMarch 13, 2008 at 3:58 pm #449409Viewing 0 reply threadsAuthorReplies-
WSHansV
AskWoody LoungerMarch 13, 2008 at 4:08 pm #1101796A line such as Set DB = Nothing can be used to release the memory used by an object variable.
In general, VBA does this automatically when you exit a procedure (sub) or function the normal way (because the code reaches End Sub or End Function, or because you execute a line Exit Sub or Exit Function.
If you encounter an unhandled error, all variables will be reset and the memory used by object variables will be released.
So in many cases, setting an object variable to Nothing is not strictly essential because it will be done automatically anyway.
But there is a bug in the way recordsets are handled – memory is not always released automatically. So it is a good idea to always set variables of type Recordset (DAO or ADO) to Nothing.
And if you don’t need an object variable any more halfway through a procedure or function, you make its memory available immediately instead of at the end of the function or procedure by setting it to Nothing. This doesn’t matter much for “small” objects, but it might really help if you open a large recordset. -
WSpatt
AskWoody Lounger -
WSHansV
AskWoody LoungerMarch 13, 2008 at 11:01 pm #1101869Closing a recordset and setting a recordset to Nothing is not the same.
Closing a recordset means that you break the connection with the database, but the basic structure is still in memory. Setting a recordset variable to nothing releases that memory.
It is always a good idea to close a recordset as soon as you don’t need it anymore, because an open recordset consumes resources and may lock things for other users.Because of the problem mentioned in my previous reply, I’ve made it a habit in Access to use error handling in any procedure/function that uses object variables, and to close recordsets etc. and set all object variables to Nothing in an exit section that is always executed. Something like this:
Sub Test()
‘ Declarations
Dim dbs As DAO.Database
Dim rst As DAO.Recordset‘ Activate error handling
On Error GoTo ErrHandler‘ Assign a value to the variables
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(“tblTest”, dbOpenDynaset)‘ Code to process the recordset goes here
…ExitHandler:
‘ The “exit handler” will always be executed, even after an error
‘ Make sure that no new errors occur
On Error Resume Next
‘ Close the recordset
rst.Close
‘ Release object variables
Set rst = Nothing
Set dbs = Nothing
‘ And get out
Exit SubErrHandler:
‘ Display a message box
MsgBox Err.Description, vbExclamation
‘ And jump to the “exit handler”
Resume ExitHandler
End Sub -
WSpatt
AskWoody Lounger
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Blocking Search (on task bar) from going to web
by
HenryW
38 minutes ago -
Windows 10: Microsoft 365 Apps will be supported up to Oct. 10 2028
by
Alex5723
5 hours, 45 minutes ago -
Add or Remove “Ask Copilot” Context Menu in Windows 11 and 10
by
Alex5723
5 hours, 52 minutes ago -
regarding april update and may update
by
heybengbeng
7 hours, 21 minutes ago -
MS Passkey
by
pmruzicka
3 hours, 26 minutes ago -
Can’t make Opera my default browser
by
bmeacham
15 hours, 2 minutes ago -
*Some settings are managed by your organization
by
rlowe44
1 hour, 45 minutes ago -
Formatting of “Forward”ed e-mails
by
Scott Mills
13 hours, 56 minutes ago -
SmartSwitch PC Updates will only be supported through the MS Store Going Forward
by
PL1
1 day, 9 hours ago -
CISA warns of hackers targeting critical oil infrastructure
by
Nibbled To Death By Ducks
1 day, 18 hours ago -
AI slop
by
Susan Bradley
8 hours, 43 minutes ago -
Chrome : Using AI with Enhanced Protection mode
by
Alex5723
1 day, 19 hours ago -
Two blank icons
by
CR2
7 hours, 39 minutes ago -
Documents, Pictures, Desktop on OneDrive in Windows 11
by
ThePhoenix
2 days, 4 hours ago -
End of 10
by
Alex5723
2 days, 7 hours ago -
Single account cannot access printer’s automatic duplex functionality
by
Bruce
1 day, 5 hours ago -
test post
by
gtd12345
2 days, 13 hours ago -
Privacy and the Real ID
by
Susan Bradley
2 days, 3 hours ago -
MS-DEFCON 2: Deferring that upgrade
by
Susan Bradley
5 hours, 58 minutes ago -
Cant log on to oldergeeks.Com
by
WSJonharnew
2 days, 17 hours ago -
Upgrading from Win 10
by
WSjcgc50
1 day, 5 hours ago -
USB webcam / microphone missing after KB5050009 update
by
WSlloydkuhnle
1 day, 9 hours ago -
TeleMessage, a modified Signal clone used by US government has been hacked
by
Alex5723
3 days, 9 hours ago -
The story of Windows Longhorn
by
Cybertooth
2 days, 21 hours ago -
Red x next to folder on OneDrive iPadOS
by
dmt_3904
3 days, 11 hours ago -
Are manuals extinct?
by
Susan Bradley
11 hours, 51 minutes ago -
Canonical ditching Sudo for Rust Sudo -rs starting with Ubuntu
by
Alex5723
3 days, 20 hours ago -
Network Issue
by
Casey H
3 days, 7 hours ago -
Fedora Linux is now an official WSL distro
by
Alex5723
4 days, 8 hours ago -
May 2025 Office non-Security updates
by
PKCano
4 days, 9 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.