Modify an embedded Connection String in microsoft excel macro

前端 未结 6 673
我在风中等你
我在风中等你 2020-12-14 19:01

I have an Excel document that has a macro which when run will modify a CommandText of that connection to pass in parameters from the Excel spreadsheet, like so:

6条回答
  •  醉话见心
    2020-12-14 19:49

    This should do the trick:

    Sub jzz()
    
    Dim conn As Variant
    Dim connectString As String
    
    For Each conn In ActiveWorkbook.Connections
        connectString = conn.ODBCConnection.Connection
        connectString = Replace(connectString, "Catalog=ADCData_Doric", "Catalog=Whatever")
        connectString = Replace(connectString, "Data Source=doric-server5", "Data Source=Whatever")
    
        conn.ODBCConnection.Connection = connectString
    Next conn
    
    
    End Sub
    

    It loops every connection in your workbook and change the Connection String (in the 2 replace statements).

    So to modify your example:

    ActiveWorkbook.Connections("Job_Cost_Code_Transaction_Summary").ODBCConnection.Connection = "new connection string"
    

提交回复
热议问题