Query Connection Strings and VBA Code to delete and connect them

FrankDTank16

New Member
Joined
Nov 25, 2015
Messages
4
I have this VBA code that i've been trying to modify. What it should do is, in this order:

Unprotect all sheets
Add Connections to all Queries (multiple databases and queries)
Refresh all queries
Remove all connections (so that the password's aren't accessible to normal excel users)
Protect all sheets (with password)
Goto Sheet that holds the button that runs macro

I'm getting an error when i run the add connections. Can anyone help me figure this out? VBA code below, i took out the full server name to protect my companies identity.



Code:
Sub AddConnectALL()
    'this will add the connection string to
    'the connections in this worksheet
   Connection1 = InStr(0, ActiveSheet.Connections, "cas_FRONGONE15_1", vbTextCompare)
   Connection2 = InStr(0, ActiveSheet.Connections, "cas_ProblemDB", vbTextCompare)
   Connection3 = InStr(0, ActiveSheet.Connections, "cas_FRONGONE", vbTextCompare)
If Connection1 >= 1 Then
ActiveSheet.Connections
        CN.ODBCConnection.BackgroundQuery = False 'this line is optional
        CN.ODBCConnection.Connection = _
        "ODBC;DRIVER={SQL Server}; Server=W8DFRONGONE\; Database=cas_FRONGONE15_1;" & _
        "UID=dba;PWD=Password;" & _
        "AnsiNPW=No" & _
        "DefaultIsolationLevel=READUNCOMMITTED"
ElseIf Connection2 >= 1 Then
ActiveSheet.Connections
        CN.ODBCConnection.BackgroundQuery = False 'this line is optional
        CN.ODBCConnection.Connection = _
        "ODBC;DRIVER={SQL Server}; Server=W8DFRONGONE\; Database=cas_ProblemDB;" & _
        "UID=dba;PWD=Password;" & _
        "AnsiNPW=No" & _
        "DefaultIsolationLevel=READUNCOMMITTED"
ElseIf Connection3 >= 1 Then
ActiveSheet.Connections
        CN.ODBCConnection.BackgroundQuery = False 'this line is optional
        CN.ODBCConnection.Connection = _
        "ODBC;DRIVER={SQL Server}; Server=W8DFRONGONE\; Database=cas_FRONGONE;" & _
        "UID=dba;PWD=Password;" & _
        "AnsiNPW=No" & _
        "DefaultIsolationLevel=READUNCOMMITTED"
End If
End Sub


Sub RemoveConnect()
    'this will remove the connection string from
    'all connections in the active workbook
    For Each CN In ActiveWorkbook.Connections
        CN.ODBCConnection.Connection = "ODBC;"
    Next
End Sub


Sub ListConnections()
    'this will list all query connection strings
    'in the active workbook
    For Each CN In ActiveWorkbook.Connections
        MsgBox (CN.ODBCConnection.Connection)
    Next
End Sub


Sub RefreshAll()
    'this will refresh all the data connections in the active workbook
    UnProtectAll
    AddConnect 'add the connection strings
    ActiveWorkbook.RefreshAll 'refresh all connections
    RemoveConnect 'remove the connection strings
    ProtectAll 'Protect all the sheets
    Application.Goto Reference:=Worksheets("Sheet1").Range("A1")
End Sub




Sub ProtectAll()
      ' Loop through all sheets in the workbook
      For i = 1 To Sheets.Count
         ' Activate each sheet in turn.
         Sheets(i).Activate
            ActiveSheet.Protect Password:="Sheets", DrawingObjects:=True, _
               Contents:=True, Scenarios:=True
      Next i
   End Sub




Public Sub UnProtectAll()
For Each Worksheet In ActiveWorkbook.Worksheets
Worksheet.Unprotect Password:="Sheets"
Next
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I would really appreciate any help. I usually don't try to build my own VBA's without internet help, but this one is really confusing me.
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

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