Re: Recordsets and datbases

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



The rule of thumb is that you should close what you opened (but only what
you opened), and set your objects to Nothing before exit.

Therefore your code should end with:
rs.Close
Set rs = Nothing
Set db = Nothing
but you do *not* db.Close because you did not open it.
You should use db.Close if you had opened another database, e.g.:
Set db = OpenDatabase ("C:\MyFolder\MyFile.mdb")

As for why: in a perfect world, the code would reliably clean up after
itself when an object went out of scope, and would fail if you closed the
wrong thing. Neither of these things happens reliably in Access.

Back in the Access 97 days, there was a bug where Access would not close,
and failing to close your recordsets was one of the causes for this bug. It
was a real pain to track down (particularly in large databases with lots of
recordsets, combined with other causes of the bug as well), so many of us
learned the lesson that explicitly closing was worth the effort.

Just as bad, if you close something you did not open (such as Currentdb),
Access just silently opens the default database again and you probably don't
even know your coding is wrong. Sometimes that does cause problems. If you
close the default workspace, Access reopens it and the default database, but
other connections you had open at the time are lost. This causes weird bugs:
e.g. the RecordsetClone of forms that were open at the time have suddenly
gone out of scope, so you get Error 91 if you try to refer to them.

Likewise, failure to set objects to Nothing can cause problems. Each time
you call CurrentDb(), Access creates another database object, and if you
don't set it to Nothing before exiting the routine, you can run into errors
about too many tables/databases open in some versions of Access.

In summary, it's always good practice to close what you open and destroy
your objects after use (or even before re-use.) Personally, I'm too lazy
*not* to do it: I have better things to do that waste my time debugging bugs
that I could have avoided by cleaning up after myself.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Steven" <Steven@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:EAEB7E9F-5193-42BB-B5DA-B915FC7DE0C1@xxxxxxxxxxxxxxxx
If I do this:

Dim vUserName, vPassword As String
Dim vAutoNumber As Double
Dim rs As DAO.Recordset
Dim db As Database
Set db = CurrentDb()
Set rs = db.OpenRecordset("Users", dbOpenDynaset)
rs.MoveFirst
rs.FindFirst BuildCriteria("[NetworkLogin]", dbText, fOSUserName)
If Not rs.NoMatch Then
If rs("AutoNumber") = 1 Then
[Forms]![Form1]![UserNumber] = vAutoNumber
End If
End If

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

Question: Do I need the:

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

What part of these do I need? What happens if they are omitted?

Thank you for your help.

Steven


.



Relevant Pages

  • open database from .vbs in maximized view
    ... opens the database but not in full view "Maximized". ... Const qdfBirthdaysDue = "Birthdays1" ... Dim dbe, db, ss ' as DBEngine, as Database, as Recordset ...
    (microsoft.public.access.modulesdaovba)
  • Re: startup option confusion w/ 2007 beta
    ... For anyone else who opens it up - only forms ... There is some stuff about the Navigation Pane at http://blogs.msdn.com/access/archive/2005/11/01/488022.aspx which shows you most of how to hide things in the Navigation Panel, including "removing" access to altering the design. ... Certainly, if you have a database protected with the new souped-up database password and encrypted with the new souped-up encryption, it is still easy to get at the data. ... Dim App as Access.Application ...
    (microsoft.public.access.security)
  • Re: Check for Exclusive
    ... Dim strFile As String ... revise and implement it into our live database. ... If a user opens the database when it was not previously open, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Security code
    ... > database into another database. ... The database opens to a ... >> Private strRole As String ... >> Dim Result As Variant ...
    (microsoft.public.access.modulesdaovba)
  • Re: Export Excel To Access Wizard HELP!!!
    ... permissions needed to run the queries needed by your workbook. ... >> write Excel code that uses the DAO library to open the secured database ... >> The sample procedure below opens a secured mdb and will work with minor ... >> Dim dbW 'As DAO.Workspace 'to change from late to ...
    (microsoft.public.access.externaldata)