Query Tables (QueryTables) in Excel 2010 with VBA with VBA creating many connections

前端 未结 8 714
北荒
北荒 2021-02-06 12:49

I\'m following code I found on another site. Here\'s the basics of my code:

Dim SQL As String
Dim connString As String

connString = \"ODBC;DSN=DB01;UID=;PWD=;Da         


        
相关标签:
8条回答
  • 2021-02-06 13:40

    I've found that by default new connections created this way are called "Connection". What I am using is this snippet of code to remove the connection but retain the listobject.

    Application.DisplayAlerts = False
    ActiveWorkbook.Connections("Connection").Delete
    Application.DisplayAlerts = True
    

    It can easily be modified to remove the latest added connection (or if you keep track of the connections by their index).

    Application.DisplayAlerts = False
    ActiveWorkbook.Connections(ActiveWorkbook.Connections.Count).Delete
    Application.DisplayAlerts = True
    
    0 讨论(0)
  • 2021-02-06 13:41

    Instead of adding another query table with the add method, you can simply update the CommandText Property of the connection. However you have to be aware that there is a bug when updating the CommandText property of an ODBC connection. If you temporarily switch to an OLEDB connection, update your CommandText property and then switch back to ODBC it does not create the new connection. Don't ask me why... this just works for me.

    Create a new module and insert the following code:

    Option Explicit
    
    Sub UpdateWorkbookConnection(WorkbookConnectionObject As WorkbookConnection, Optional ByVal CommandText As String = "", Optional ByVal ConnectionString As String = "")
    
    With WorkbookConnectionObject
        If .Type = xlConnectionTypeODBC Then
            If CommandText = "" Then CommandText = .ODBCConnection.CommandText
            If ConnectionString = "" Then ConnectionString = .ODBCConnection.Connection
            .ODBCConnection.Connection = Replace(.ODBCConnection.Connection, "ODBC;", "OLEDB;", 1, 1, vbTextCompare)
        ElseIf .Type = xlConnectionTypeOLEDB Then
            If CommandText = "" Then CommandText = .OLEDBConnection.CommandText
            If ConnectionString = "" Then ConnectionString = .OLEDBConnection.Connection
        Else
            MsgBox "Invalid connection object sent to UpdateWorkbookConnection function!", vbCritical, "Update Error"
            Exit Sub
        End If
        If StrComp(.OLEDBConnection.CommandText, CommandText, vbTextCompare) <> 0 Then
            .OLEDBConnection.CommandText = CommandText
        End If
        If StrComp(.OLEDBConnection.Connection, ConnectionString, vbTextCompare) <> 0 Then
            .OLEDBConnection.Connection = ConnectionString
        End If
        .Refresh
    End With
    
    End Sub
    

    This UpdateWorkbookConnection subroutine only works on updating OLEDB or ODBC connections. The connection does not necessarily have to be linked to a pivot table. It also fixes another problem and allows you to update the connection even if there are multiple pivot tables based on the same connection.

    To initiate the update just call the function with the connection object and command text parameters like this:

    UpdateWorkbookConnection ActiveWorkbook.Connections("Connection"), "exec sp_MyAwesomeProcedure"
    

    You can optionally update the connection string as well.

    0 讨论(0)
提交回复
热议问题