Home All Groups Group Topic Archive Search About

re-using ADODB.Connection and Recordset objects

Author
10 May 2007 11:37 PM
mike7411@gmail.com
I was just wondering if it is okay to re-use ADODB.Connection and
Recordset objects in Visual Basic 6.

For instance, is this okay?

Dim adoConn As New ADODB.Connection
adoConn.ConnectionString = siteA
adoConn.Open
adoConn.Close

adoConn.ConnectionString = siteB
adoConn.Open
adoConn.Close

Or, should you do this?


Dim adoConn As New ADODB.Connection
adoConn.ConnectionString = siteA
adoConn.Open
adoConn.Close

Set adoConn = Nothing

adoConn.ConnectionString = siteB
adoConn.Open
adoConn.Close

I am seeing a strange crashing behavior that appears to be related to
use of the former, but theoretically I'm not sure there's a problem
with it.

Author
10 May 2007 11:59 PM
Robert Morley
There's nothing wrong with either one, really, but the caveat if you're not
setting it to Nothing is that you will inherit most of the properties from
the previous connection or recordset.  As long as you make sure to reset any
properties you need to change between the two, then you should be okay.

So lets say you open siteA and tell it to default to client-side cursors
(adoConn.CursorLocation = adUseClient), then when you open siteB, it will
also use client-side cursors, unless you explicitly tell it not to.



Rob

<mike7***@gmail.com> wrote in message
Show quoteHide quote
news:1178840223.395287.235580@q75g2000hsh.googlegroups.com...
>I was just wondering if it is okay to re-use ADODB.Connection and
> Recordset objects in Visual Basic 6.
>
> For instance, is this okay?
>
> Dim adoConn As New ADODB.Connection
> adoConn.ConnectionString = siteA
> adoConn.Open
> adoConn.Close
>
> adoConn.ConnectionString = siteB
> adoConn.Open
> adoConn.Close
>
> Or, should you do this?
>
>
> Dim adoConn As New ADODB.Connection
> adoConn.ConnectionString = siteA
> adoConn.Open
> adoConn.Close
>
> Set adoConn = Nothing
>
> adoConn.ConnectionString = siteB
> adoConn.Open
> adoConn.Close
>
> I am seeing a strange crashing behavior that appears to be related to
> use of the former, but theoretically I'm not sure there's a problem
> with it.
>
Author
11 May 2007 12:00 AM
Bob Butler
<mike7***@gmail.com> wrote in message
Show quoteHide quote
news:1178840223.395287.235580@q75g2000hsh.googlegroups.com...
>I was just wondering if it is okay to re-use ADODB.Connection and
> Recordset objects in Visual Basic 6.
>
> For instance, is this okay?
>
> Dim adoConn As New ADODB.Connection
> adoConn.ConnectionString = siteA
> adoConn.Open
> adoConn.Close
>
> adoConn.ConnectionString = siteB
> adoConn.Open
> adoConn.Close

That's fine although you should probably not use As New:
Dim adoConn As ADODB.Connection
Set adoConn = New ADODB.Connection

It generates more efficient code and helps avoid some unexpected bugs when
you try to access objects.

You don't have to set the connection to Nothing before opening another.