0

This is my reference to Before/After Query Refresh Event

To import data from SQL Server, I have a parameterized query that requires for a date input from the user.

  1. When the user enters the beginning date and end date correctly, everything is completed in the correct order, and the case is closed.

  2. However, when the date is not correct, the error handler is not triggered, and the macro continues until the AfterRefresh event.

I added error handlers to every module, but they are still not responding. Does anyone know how to add an error handler to a Before/After Refresh Event?

The logic of the power query code is as follows, but not written in this manner:

let 
    Source = Sql.Database("MyDataBase", [Query="Select Transaction_date, Customer_ID
                                                from TransactionList
                                                where Transaction_date between @StartDate and @EndDate]
in
    Source

VBA Code below

'========== [ Modules ] ========== 
Option Explicit 
Dim colQueries As New Collection

Sub Button_RefreshData() 
On Error GoTo Error_Handler

    Call InitializeQueries
    ThisWorkbook.RefreshAll
Exit Sub
Error_Handler: 
    MsgBox Err.Number & " " & Err.Source & vbNewLine & Err.Description, vbInformation 
    Exit Sub 
End Sub

Sub InitializeQueries() 
On Error GoTo Error_Handler
Dim clsQ As clsQuery 
Dim WS As Worksheet 
Dim QT As QueryTable 
Dim LO As ListObject

For Each WS In ThisWorkbook.Worksheets 
    For Each QT In WS.QueryTables 
        Set clsQ = New clsQuery 
        Set clsQ.MyQuery = QT 
        colQueries.Add clsQ 
    Next QT 
    For Each LO In WS.ListObjects 
        Set QT = LO.QueryTable 
        Set clsQ = New clsQuery 
        Set clsQ.MyQuery = QT 
        colQueries.Add clsQ 
    Next LO 
Next WS
Exit Sub
Error_Handler: 
    MsgBox Err.Number & " " & Err.Source & vbNewLine & Err.Description, vbInformation 
    Exit Sub 
End Sub
'========== [ Class Modules named clsQuery ] ========== 
Option Explicit 
Public WithEvents MyQuery As QueryTable

Private Sub MyQuery_AfterRefresh (ByVal Success As Boolean) 
On Error GoTo Error_Handler
    If Success Then MsgBox ("The entire process is complete.") 
Exit Sub
Error_Handler: 
    MsgBox Err.Number & " " & Err.Source & vbNewLine & Err.Description, vbInformation 
    Exit Sub 
End Sub

Private Sub MyQuery_BeforeRefresh (Cancel As Boolean) 
On Error GoTo Error_Handler
    MsgBox ("The process start now.") 
Exit Sub
Error_Handler: 
    MsgBox Err.Number & " " & Err.Source & vbNewLine & Err.Description, vbInformation 
    Exit Sub 
End Sub
6
  • The main problem I can see is that your error handler is in the main body of code. The error handler should be at the end of your code after an Exit Sub and before the End Sub. I'm don't think that will stop your On Error Goto line from working, but it will try and display an error message at the end of every procedure whether an error occurred or not. Commented Oct 11, 2023 at 7:09
  • From my own experience, errors fired by Power Query cannot be captured by VBA. I'll try to find out if we applied an alternative "inventive" solution Commented Oct 11, 2023 at 8:19
  • @DarrenBartrup-Cook Sorry for the mistake in my question. I actually placed my error handler before Exit Sub. I will revise my code on the question. But still the error handler is not triggered. Commented Oct 11, 2023 at 9:26
  • @CHill60 In my experience, error handlers can capture errors on power queries when the before/after refresh event is not used. I dont know about error handler to a Before/After Refresh Event. Thank you for sharing Commented Oct 11, 2023 at 9:31
  • The error handler in the before refresh won't be fired because the error is not occurring before the refresh. Same with the after refresh - the error is happening during the refresh - and I've never seen on error in VBA respond to that. If you have - great, that's where you should be looking. But even this fails to capture the error for me Sub demo() On Error GoTo X ThisWorkbook.RefreshAll Exit Sub X: MsgBox "never gets here" End Sub Commented Oct 11, 2023 at 16:17

1 Answer 1

0

However, when the date is not correct - you say. Then you should consider testing your users input before using it in your query. A way to do this is to use CDate to validate if it is a valid date.

Try this as your user input and validation:

Sub InputDates()
' With test of valid date

TryAgainFrom:
    On Error GoTo ErrHandlerFromDate
    FromDate = CDate(InputBox("Input a from date:", "From:", Format(Now() - 365, "DD.MM.YYYY")))

TryAgainTo:
    On Error GoTo ErrHandlerToDate
    ToDate = CDate(InputBox("Input a to date:", "To:", Format(Now() - 365, "DD.MM.YYYY")))

' Bouth dates are valid dates
MsgBox ("Range from " & Format(FromDate, "dd.mm.yyyy") & " to " & Format(ToDate, "dd.mm.yyyy"))

Exit Sub
ErrHandlerFromDate:
    MsgBox "Your input From date is not a valid date! - Please try again."
    Resume TryAgainFrom

ErrHandlerToDate:
    MsgBox "Your input To date is not a valid date! - Please try again."
    Resume TryAgainTo

End Sub


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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.