【问题标题】:Excel VBA; Updating a connection stringExcel VBA;更新连接字符串
【发布时间】:2013-12-29 12:56:33
【问题描述】:

我只是想让 VBA 更新 OLEDB 连接字符串。当我单步执行代码时,我没有收到任何错误,但连接刷新失败,当我检查 UI 中的连接字符串时,很明显我的代码根本没有更改它(因此刷新失败)。我错过了什么?

代码如下:

Sub UpdateQueryConnectionString(ConnectionString As String)

  With ActiveWorkbook.Connections("Connection Name"). _
      OLEDBConnection
      .Connection = StringToArray(ConnectionString)
  End With
  ActiveWorkbook.Connections("Connection Name").Refresh
End Sub

输入的 ConnectionString 是:

ConnectionString = = "Provider=SLXOLEDB.1;Data Source=SERVER;Initial Catalog=DATABASE" _
& ";User ID=" & Username & ";Password=" & Password & _
";Persist Security Info=True;Extended Properties=" _
& Chr(34) & "PORT=1706;LOG=ON;CASEINSENSITIVEFIND=ON;INCLUDECALCFIELDS=ON;" & Chr(34)

函数 StringToArray 直接从 http://support.microsoft.com/kb/105416 上的示例 4 中复制

【问题讨论】:

  • 我建议您仔细检查 locals 窗口中的每个属性以确保它们确实存在 - 我不知道语法。考虑到 ConnectionString 是一个字符串这一事实,Array(ConnectionString) 似乎是一种奇怪的语法。
  • @KimGysen 似乎是对的 - 这是什么 array(),尝试不使用 array(),只使用 ConnectionString
  • 我认为数组部分是由宏记录器生成的,正如@KimGysen 所说,不适用于此处。只需 ConnectionString 即可尝试。
  • 我在没有 Array() 的情况下尝试过,但出现运行时错误。一点点挖掘发现了这个MS Support article。请参见示例 4。该数组用于阻止超过 255 个字符的字符串被截断,但我认为我做的不对。我会将问题中的代码更新为我现在所做的。 UI 仍然没有显示对连接字符串的更改(正确或错误)。
  • 我没有计算字符数,但您的连接字符串看起来没有超过 255 个字符的限制?

标签: vba excel connection-string excel-2010


【解决方案1】:

知道了。以下代码有效。

Sub UpdateQueryConnectionString(ConnectionString As String)

  Dim cn As WorkbookConnection
  Dim oledbCn As OLEDBConnection
  Set cn = ThisWorkbook.Connections("Connection Name")
  Set oledbCn = cn.OLEDBConnection
  oledbCn.Connection = ConnectionString

End Sub

只需将 ConnectionString 作为字符串输入,就像我在最初的问题中说明的那样。

【讨论】:

    【解决方案2】:

    这一行适用于我刷新使用 OLEDB 的代码:

    ActiveWorkbook.Connections("Connection Name").OLEDBConnection.Refresh
    

    原因似乎是 excel 要求您指明类型,即使您引用的是特定的命名连接。

    【讨论】:

    • 刷新工作,但 UI 仍在报告我通过 UI 输入的连接字符串。不是 VBA 应该插入的连接字符串。
    • 我检查了字符串,238 个字符,258 个带空格...你能删除 CHR(34) [双引号] - 有必要吗?
    • 是的,CHR(34) 是连接字符串的必需部分。这是我发现将 " 作为字符串的一部分进行转义的最简单方法。不,我没有尝试获取连接文件。
    【解决方案3】:

    我们甚至可以刷新特定的连接,然后它会刷新所有链接到它的枢轴。

    对于这段代码,我从 Excel 中的表格创建了切片器:

    Sub UpdateConnection()
        Dim ServerName As String
        Dim ServerNameRaw As String
        Dim CubeName As String
        Dim CubeNameRaw As String
        Dim ConnectionString As String
    
        ServerNameRaw = ActiveWorkbook.SlicerCaches("Slicer_ServerName").VisibleSlicerItemsList(1)
        ServerName = Replace(Split(ServerNameRaw, "[")(3), "]", "")
    
        CubeNameRaw = ActiveWorkbook.SlicerCaches("Slicer_CubeName").VisibleSlicerItemsList(1)
        CubeName = Replace(Split(CubeNameRaw, "[")(3), "]", "")
    
        If CubeName = "All" Or ServerName = "All" Then
            MsgBox "Please Select One Cube and Server Name", vbOKOnly, "Slicer Info"
        Else
            ConnectionString = GetConnectionString(ServerName, CubeName)
            UpdateAllQueryTableConnections ConnectionString, CubeName
        End If
    End Sub
    
    Function GetConnectionString(ServerName As String, CubeName As String)
        Dim result As String
        result = "OLEDB;Provider=MSOLAP.5;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=" & CubeName & ";Data Source=" & ServerName & ";MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error;Update Isolation Level=2"
        '"OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=" & CubeName & ";Data Source=" & ServerName & ";Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False"
        GetConnectionString = result
    End Function
    
    Sub UpdateAllQueryTableConnections(ConnectionString As String, CubeName As String)
        Dim cn As WorkbookConnection
        Dim oledbCn As OLEDBConnection
        Dim Count As Integer, i As Integer
        Dim DBName As String
        DBName = "Initial Catalog=" + CubeName
    
        Count = 0
        For Each cn In ThisWorkbook.Connections
            If cn.Name = "ThisWorkbookDataModel" Then
                Exit For
            End If
    
            oTmp = Split(cn.OLEDBConnection.Connection, ";")
            For i = 0 To UBound(oTmp) - 1
                If InStr(1, oTmp(i), DBName, vbTextCompare) = 1 Then
                    Set oledbCn = cn.OLEDBConnection
                    oledbCn.SavePassword = True
                    oledbCn.Connection = ConnectionString
                    Count = Count + 1
                End If
            Next
        Next
    
        If Count = 0 Then
             MsgBox "Nothing to update", vbOKOnly, "Update Connection"
        ElseIf Count > 0 Then
            MsgBox "Connection Updated Successfully", vbOKOnly, "Update Connection"
        End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-17
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 2012-09-26
      • 2014-01-27
      相关资源
      最近更新 更多