I have created a Ribbon button to switch the "MaintainConnection" property of the (first) pivot table's OBDC on the active worksheet. This throws an error when the pivot table's source is local, I assume because the property doesn't exist.
Currently I have the code set to exit when any error occurs, but I would like to check whether the "MaintainConnection" property exists (i.e. the pivot tables datasource is an OBDC) and then exit if it does not exist, plus a msgbox.
Does anyone know how I check can check if the property exists before changing it?
Currently I have the code set to exit when any error occurs, but I would like to check whether the "MaintainConnection" property exists (i.e. the pivot tables datasource is an OBDC) and then exit if it does not exist, plus a msgbox.
Does anyone know how I check can check if the property exists before changing it?
Code:
Sub ODBC_PT_StayConnected()
Dim ws As Worksheet
Dim pt As PivotTable
Dim pc As PivotCache
On Error GoTo Exit_Sub
Set ws = ActiveSheet
Set pt = ws.PivotTables(1)
Set pc = pt.PivotCache
'If function here to check if property MaintainConnection exists, or pt has external datasource (OBDC)
If MsgBox(pt & "'s ODBC MaintainConnection is currently " & pc.MaintainConnection & "." & vbNewLine & _
"Switch to " & (Not pc.MaintainConnection) & "?", vbYesNo) _
= vbYes Then
pc.MaintainConnection = Not pc.MaintainConnection
MsgBox pt & "'s ODBC MaintainConnection has been set to " & pc.MaintainConnection
End If
Exit_Sub:
End Sub