CurrentDB

M

Max Yaffe

Is it necessary or recommended to close a database object initialized
with "CurrentDb", e.g.

1) Dim DB as Database
2) Set DB = CurrentDb
...
3) DB.Close
4) Set DB = Nothing

Is statement 3 necessary? statement 4?

Thanks
Max
 
R

Rick Brandt

Max said:
Is it necessary or recommended to close a database object initialized
with "CurrentDb", e.g.

1) Dim DB as Database
2) Set DB = CurrentDb
...
3) DB.Close
4) Set DB = Nothing

Is statement 3 necessary? statement 4?

Thanks
Max

Keep 4, lose 3. 3 is harmless since it actually does nothing. If it actually
DID close CurrentDB it would be harmful.
 
G

Guest

2) Set DB = CurrentDb
4) Set DB = Nothing

If you are using a variable with global scope then that may
be important. Common MS examples always do that, so
that the example can be used generally, particularly in ASP.

But in VBA, Set DB = Nothing is implicit in End Sub or
Exit Sub.

In Access, if the next line is End Sub (or Exit Sub, or other
code, then End/Exit sub), then your line 4 is not required.
2) Set DB = CurrentDb
3) DB.Close

Line 2 does not add a new database to the databases
collection. If it did, you would want to close the new
database object, which you could do by using Close,
or by setting all of the references to Nothing, or by letting
all of the references go out of scope.

If you use OpenDatabase with ASP, you would want
to close the database and set the reference to nothing.

If you want to write an OpenDatabase example for
general use, you would want to close the database and
set the reference to nothing.

But if you are using Access VBA subroutines with
CurrentDB, those two lines just make your code longer.

(david)
 
T

TC

A simple rule, is "close what you opened".

In your example, you didn't .Open DB, so you shouldn't/needn't .Close
it either.

HTH,
TC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top