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
-
Cox Communications and Charter Communications to merge
by
not so anon
5 hours, 49 minutes ago -
Help with WD usb driver on Windows 11
by
Tex265
4 hours, 57 minutes ago -
hibernate activation
by
e_belmont
8 hours, 42 minutes ago -
Red Hat Enterprise Linux 10 with AI assistant
by
Alex5723
12 hours, 30 minutes ago -
Windows 11 Insider Preview build 26200.5603 released to DEV
by
joep517
15 hours, 35 minutes ago -
Windows 11 Insider Preview build 26120.4151 (24H2) released to BETA
by
joep517
15 hours, 36 minutes ago -
Fixing Windows 24H2 failed KB5058411 install
by
Alex5723
18 hours, 46 minutes ago -
Out of band for Windows 10
by
Susan Bradley
20 hours, 20 minutes ago -
Giving UniGetUi a test run.
by
RetiredGeek
1 day, 3 hours ago -
Windows 11 Insider Preview Build 26100.4188 (24H2) released to Release Preview
by
joep517
1 day, 10 hours ago -
Microsoft is now putting quantum encryption in Windows builds
by
Alex5723
1 day, 8 hours ago -
Auto Time Zone Adjustment
by
wadeer
1 day, 15 hours ago -
To download Win 11 Pro 23H2 ISO.
by
Eddieloh
1 day, 13 hours ago -
Manage your browsing experience with Edge
by
Mary Branscombe
17 hours, 39 minutes ago -
Fewer vulnerabilities, larger updates
by
Susan Bradley
6 hours, 4 minutes ago -
Hobbies — There’s free software for that!
by
Deanna McElveen
12 hours, 29 minutes ago -
Apps included with macOS
by
Will Fastie
10 hours, 20 minutes ago -
Xfinity home internet
by
MrJimPhelps
7 hours, 8 minutes ago -
Convert PowerPoint presentation to Impress
by
RetiredGeek
1 day, 8 hours ago -
Debian 12.11 released
by
Alex5723
2 days, 12 hours ago -
Microsoft: Troubleshoot problems updating Windows
by
Alex5723
2 days, 16 hours ago -
Woman Files for Divorce After ChatGPT “Reads” Husband’s Coffee Cup
by
Alex5723
1 day, 19 hours ago -
Moving fwd, Win 11 Pro,, which is best? Lenovo refurb
by
Deo
1 hour, 46 minutes ago -
DBOS Advanced Network Analysis
by
Kathy Stevens
3 days, 9 hours ago -
Microsoft Edge Launching Automatically?
by
healeyinpa
2 days, 23 hours ago -
Google Chrome to block admin-level browser launches for better security
by
Alex5723
21 hours, 15 minutes ago -
iPhone SE2 Stolen Device Protection
by
Rick Corbett
3 days, 4 hours ago -
Some advice for managing my wireless internet gateway
by
LHiggins
2 days, 11 hours ago -
NO POWER IN KEYBOARD OR MOUSE
by
HE48AEEXX77WEN4Edbtm
1 day, 13 hours ago -
A CVE-MITRE-CISA-CNA Extravaganza
by
Nibbled To Death By Ducks
3 days, 21 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.