3

I'm just trying to get VBA to update an OLEDB connection string. When I step through the code, I don't get any errors but the connection refresh fails and when I examine the connection string in the UI, it's obvious that my code has not changed it at all (hence the refresh failure). What have I missed?

Here is the code:

Sub UpdateQueryConnectionString(ConnectionString As String)

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

The ConnectionString being fed in is:

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)

The function StringToArray is copied straight out of Example 4 on http://support.microsoft.com/kb/105416

9
  • 2
    I suggest that you carefully check each of the properties in the locals window to make sure that they actually exist - I don't know the syntax by heart. Array(ConnectionString) seem like weird syntax, considering the fact that ConnectionString is a string. Commented Dec 11, 2013 at 14:42
  • 1
    @KimGysen seems to be right- what is this array() for, try without array(), just with ConnectionString. Commented Dec 11, 2013 at 14:44
  • 1
    I think the array part is generated by the Macro Recorder, and as @KimGysen says, doesn't apply here. Try it with just ConnectionString. Commented Dec 11, 2013 at 14:45
  • I tried it without the Array() but get a runtime error. A little digging found this MS Support article. See Example 4. The array is used to stop strings over 255 characters being truncated but I don't think I did this right. I'll update the code in my question to what I've done now. The UI still shows no changes to the Connection String (right or wrong). Commented Dec 11, 2013 at 15:38
  • I haven't counted the number of characters but your connection string doesn't look like it crosses 255 character limit? Commented Dec 11, 2013 at 15:45

3 Answers 3

2

Got it. The following code has worked.

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

Just feed ConnectionString in as a string like I illustrated in my initial question.

Sign up to request clarification or add additional context in comments.

Comments

0

This line works for me to refresh code that uses OLEDB:

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

The reason seems to be that excel requires you to indicate the type even if you are referencing a specific, named, connection.

3 Comments

The refresh works but the UI is still reporting the connection string I entered through the UI. Not the connection string that the VBA should have inserted.
I checked the string, 238 characters, 258 with spaces...can you remove the CHR(34) [double-quote] - is that necessary?
yes, the CHR(34) is a required part of the connection string. It was the easiest way I found to escape the " as part of the string. No, I haven't tried getting a connection file.
0

Even we can refresh particular connection and in turn it will refresh all the pivots linked to it.

For this code I have created slicer from table present in 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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.