Dear experts, I am doing some PoC test for a PowerQuery project via VBA. Here are the requirement
1. Need to refresh the queries in a given order
2. The query behind needs to start once the previous query finishes and successes, if fails, need to provide error reason. Msgbox(query name, refresh status, error reason if applied)
I tried to use AfterRefresh event But I couldn't get the individual query refresh status, but overall status. And I tried to disable the background fresh , but didn't find a way if it worked or not. Please could I have your help. Thanks in advance,(I found the BackgroundQuery=True must be set. otherwise, if it set to False the afterfresh event will not be triggered. I got lost...) Here are the codes:
1. Class Modules( I expect to show the queryname in the Msgbox, how could add ?)
------------------------------------------------------------------------
Option Explicit
Public WithEvents MyQuery As QueryTable
Private Sub MyQuery_AfterRefresh(ByVal Success As Boolean)
If Success Then
MsgBox "Query has been refreshed."
Else
End If
End Sub
Private Sub MyQuery_BeforeRefresh(Cancel As Boolean)
If MsgBox("Refresh query?", vbYesNo) = vbNo Then
Cancel = True
Else
End If
End Sub
---------------------------------------------------------------------
2. Modules
-------------------------------------------------------------------
Dim QT As QueryTable
Dim LO As ListObject
For Each WS In ThisWorkbook.Worksheets
For Each QT In WS.QueryTables
MsgBox (WS.Name)
Set clsQ = New clsQuery
Set clsQ.MyQuery = QT
colQueries.Add clsQ
Next QT
For Each LO In WS.ListObjects
MsgBox (WS.Name)
Set QT = LO.QueryTable
Set clsQ = New clsQuery
Set clsQ.MyQuery = QT
colQueries.Add clsQ
Next LO
Next WS
End Sub
Private Sub Button1_Click()
Call InitializeQueries
'ActiveWorkbook.Connections("Query - CombineErrorlists").Refresh
Dim bRfresh As Boolean
With ThisWorkbook.Connections("Query - CombineErrorlists").OLEDBConnection
bRfresh = .BackgroundQuery
.BackgroundQuery = False
.Refresh
.BackgroundQuery = bRfresh
End With
' Application.Wait DateAdd("s", 15, Now)
ActiveWorkbook.Connections("Query - FilesTransform").Refresh
ActiveWorkbook.Connections("Query - thirdQuery").Refresh
End Sub
----------------------------------------------------------------------------------------------------
1. Need to refresh the queries in a given order
2. The query behind needs to start once the previous query finishes and successes, if fails, need to provide error reason. Msgbox(query name, refresh status, error reason if applied)
I tried to use AfterRefresh event But I couldn't get the individual query refresh status, but overall status. And I tried to disable the background fresh , but didn't find a way if it worked or not. Please could I have your help. Thanks in advance,(I found the BackgroundQuery=True must be set. otherwise, if it set to False the afterfresh event will not be triggered. I got lost...) Here are the codes:
1. Class Modules( I expect to show the queryname in the Msgbox, how could add ?)
------------------------------------------------------------------------
Option Explicit
Public WithEvents MyQuery As QueryTable
Private Sub MyQuery_AfterRefresh(ByVal Success As Boolean)
If Success Then
MsgBox "Query has been refreshed."
Else
End If
End Sub
Private Sub MyQuery_BeforeRefresh(Cancel As Boolean)
If MsgBox("Refresh query?", vbYesNo) = vbNo Then
Cancel = True
Else
End If
End Sub
---------------------------------------------------------------------
2. Modules
-------------------------------------------------------------------
Dim QT As QueryTable
Dim LO As ListObject
For Each WS In ThisWorkbook.Worksheets
For Each QT In WS.QueryTables
MsgBox (WS.Name)
Set clsQ = New clsQuery
Set clsQ.MyQuery = QT
colQueries.Add clsQ
Next QT
For Each LO In WS.ListObjects
MsgBox (WS.Name)
Set QT = LO.QueryTable
Set clsQ = New clsQuery
Set clsQ.MyQuery = QT
colQueries.Add clsQ
Next LO
Next WS
End Sub
Private Sub Button1_Click()
Call InitializeQueries
'ActiveWorkbook.Connections("Query - CombineErrorlists").Refresh
Dim bRfresh As Boolean
With ThisWorkbook.Connections("Query - CombineErrorlists").OLEDBConnection
bRfresh = .BackgroundQuery
.BackgroundQuery = False
.Refresh
.BackgroundQuery = bRfresh
End With
' Application.Wait DateAdd("s", 15, Now)
ActiveWorkbook.Connections("Query - FilesTransform").Refresh
ActiveWorkbook.Connections("Query - thirdQuery").Refresh
End Sub
----------------------------------------------------------------------------------------------------