Closing Data connections using VBA

xigrix

New Member
Joined
Feb 28, 2005
Messages
15
Hi

I've written spreadsheet which reads and writes data from an access database.

I think I've got some poor code though - when I click on the data Connections in the Data toolbar it takes an age to load and has thousands of entries - I think this is slowing down the whole workbook (opening and closing times are very lengthy)

Does anyone know a method by which I can close these en masse using VBA?

I'm guessing there's a for each collection I can use but I'm not sure which one

Thanks

xigrix
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Sub myPass(sql)

'update the sql
If ThisWorkbook.Worksheets("Params2").Cells(2, 9) = True Then
ThisWorkbook.Worksheets("Params2").Range("A3:D1048576").Clear
End If
myRow = [updateLog]
myStart = Now
ThisWorkbook.Worksheets("params2").Cells(myRow, 1) = sql


Application.StatusBar = sql

Dim objCommand As ADODB.Command
Dim rsData As ADODB.Recordset
Dim lRecordsAffected As Long
Dim lKey As Long
Dim sConnect As String
myLocation = ThisWorkbook.Worksheets("params2").Cells(1, 9)

On Error GoTo ErrorHandler

' Create the connection string.
sConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & myLocation & ";" & _
"Mode=Share Exclusive"

' Create the Command object we'll use for all three queries.
Set objCommand = New ADODB.Command
objCommand.ActiveConnection = sConnect


objCommand.CommandText = sql

'Execute the SQL statement.
objCommand.Execute RecordsAffected:=lRecordsAffected, _
Options:=adCmdText Or adExecuteNoRecords

ThisWorkbook.Worksheets("params2").Cells(myRow, 2) = lRecordsAffected
ThisWorkbook.Worksheets("params2").Cells(myRow, 3) = myStart
ThisWorkbook.Worksheets("params2").Cells(myRow, 4) = DateDiff("s", myStart, Now)

Application.StatusBar = False

ErrorExit:

' Destroy our ADO objects.


Set objCommand = Nothing
Set rsData = Nothing
ThisWorkbook.Worksheets("Params").Activate
Exit Sub

ErrorHandler:
If Err.Number <> -2147467259 Then

' MsgBox Err.Description, vbCritical
Else
Set objCommand = Nothing
Set rsData = Nothing
ThisWorkbook.Worksheets("Params").Activate
Exit Sub
End If
Resume ErrorExit

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top