Public Sub ChangeConnections()
Dim strConnection As String
Dim i As Integer
Dim strServer As String
Dim strDatabase As String
Dim strLogin As String
Dim strPassword As String
Dim strSQL As String
' should add code to make sure each of these have a value
' should also add error handling code
strServer = Range("Server").Value
strDatabase = Range("database").Value
strLogin = Range("login").Value
strPassword = Range("password").Value
'build the connection string
strConnection = "ODBC;DRIVER=SQL Server;SERVER=" & strServer & _
";ID=" & strLogin & ";Password=" & strPassword & ";Database=" & strDatabase & _
";Trusted_Connection=False;"
For i = 1 To ActiveWorkbook.Connections.Count 'loop through all connections
Debug.Print i & ": " & ActiveWorkbook.Connections(i).Name
Debug.Print ActiveWorkbook.Connections(i).ODBCConnection.Connection
Debug.Print ActiveWorkbook.Connections(i).ODBCConnection.CommandText
Debug.Print "=============================================="
If i = 4 Then 'test update the cost center connection
' command text must remove the reference to the database name
strSQL = "SELECT CCLNUM, CCLNAM, CCLDES " & _
"FROM SCMCCLI " & _
"ORDER BY CCLNUM"
ActiveWorkbook.Connections(i).ODBCConnection.CommandText = strSQL
ActiveWorkbook.Connections(i).ODBCConnection.Connection = strConnection
End If
Next
End Sub