Home > Back-end >  Refresh power query VBA
Refresh power query VBA

Time:12-06

So I use the below code to refresh my Query connections, however, how can I display a message if my refresh failed due to wtv reason? as this VBA shows me refresh complete even though there are multiple errors with my queries.

'Worksheets("Details").Unprotect
 Dim Connection As WorkbookConnection
    Dim bugfix As Integer
    
        For bugfix = 1 To 2

            On Error Resume Next
            
            For Each Connection In ActiveWorkbook.Connections
            
                With Connection
                
                    If (.Type = xlConnectionTypeODBC) Then
                        .ODBCConnection.BackgroundQuery = False
                    
                    Else
                    
                        If (.Type = xlConnectionTypeOLEDB) Then
                            .OLEDBConnection.BackgroundQuery = False
                    
                        End If
                    
                    End If
                
                End With
            
            Connection.Refresh
            
            Next Connection
            
        Next bugfix
'Worksheets("Details").Protect , AllowFiltering:=True, AllowFormattingCells:=True, DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingColumns:=True
MsgBox "Refresh Complete"
End Sub

CodePudding user response:

Belive it will stop at Connection.Refresh - with the Debugger. And if you want to catch this Error in your Sub you need to define something like On Error Goto MyError and then handle the Error with a message or something. Resume Next will then continue your code on the next line of your error.

Exit Sub
MyError:
MsgBox (Err.Description)
resume next
End Sub

Described here at: Microsoft Learn

  • Related