I use many ado connection in my Excel VBA project.
I need to be sure to close and set nothing all active ADO connection.?
Possible?
POSSIBLE to close all active ADO connection
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78446
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: POSSIBLE to close all active ADO connection
There is no built-in collection of all active connections, so unless you create such a collection yourself, there is no way to loop through all connections.
See How to Close All Active Connections of ADO?
See How to Close All Active Connections of ADO?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
Re: POSSIBLE to close all active ADO connection
HansV wrote:There is no built-in collection of all active connections, so unless you create such a collection yourself, there is no way to loop through all connections.
See How to Close All Active Connections of ADO?
Tks Hans, but no effect!
Code: Select all
Sub CHECK_CONNECTION()
Dim m_colConns As Collection
'...Somewhere in your code
'm_colConns.Add cnMyConnection
'...Now in your unload event
If Not m_colConns Is Nothing Then
Dim cn 'you want a variant here
For Each cn In m_colConns
cn.Close
Set cn = Nothing
Next cn
End If
End Sub
-
- Administrator
- Posts: 78446
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: POSSIBLE to close all active ADO connection
You have to use the line
m_colConns.Add cnMyConnection
each time you open a connection cnMyConnection. For example:
Dim m_colConns As Collection
Dim CONN1 As ADODB.Connection
Dim CONN2 As ADODB.Connection
...
Set m_colConns = New Collection
...
Set CONN1 = New ADODB.Connection
CONN1.Open ...
m_colConns.Add CONN1
...
Set CONN2 = New ADODB.Connection
CONN2.Open ...
m_colConns.Add CONN2
Then, at the end of your code:
Dim cn As Variant
For Each cn In m_colConns
cn.Close
Set cn = Nothing
Next cn
m_colConns.Add cnMyConnection
each time you open a connection cnMyConnection. For example:
Dim m_colConns As Collection
Dim CONN1 As ADODB.Connection
Dim CONN2 As ADODB.Connection
...
Set m_colConns = New Collection
...
Set CONN1 = New ADODB.Connection
CONN1.Open ...
m_colConns.Add CONN1
...
Set CONN2 = New ADODB.Connection
CONN2.Open ...
m_colConns.Add CONN2
Then, at the end of your code:
Dim cn As Variant
For Each cn In m_colConns
cn.Close
Set cn = Nothing
Next cn
Best wishes,
Hans
Hans