
Originally Posted by
Dion
I am wondering if there is a preferred way of creating objects (if that is
the right word). For example, I am using:
Dim cnn1 As ADODB.Connection
Set cnn1 = New ADODB.Connection
I know I could also use:
Dim cnn1 As New ADODB.Connection
I understand both do the same thing? Are there scenarios where one is
preferred
over the other, or simply a personal choice?
Also, I am more interested in the proper use of "close" and "set object to
nothing"
In my above code, at the end I have:
cnn1.Close
Is this all I need to do? Should I also have:
Set cnn1 = Nothing
Does using close without set = nothing leave anything open?
Thanks!
Dion
The two line one is preffered but only because it's easier to read
You shoul use both .close (first) and set to nothing.
Why?
.close let's vba know the connection is closed and some buffers and error checking is done. But as Microsoft is Microsoft due to a glitch it does not relaase the memory used by this variable ans setting to nothing reeases this chunk of memory.
Setting to nothing without closing can lock up your database becaus a record set wasn't closed
Rule ot thumb:
close and set to nothing objects in the reverse order you've have set them.
so first the recordset(s) and then the database for example.
Bookmarks