Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Centaur
Helper V
Helper V

VBA Code to Refresh Power Query

Hello,

 

I am importing an excel file's power query output into MS Access. 

I am wondering if I can add code to the below to refresh the Power Query automatically by VBA prior to importing into Access?  I sometimes forget to refresh the Power Query. 

 

For example, I have the following code on a button's click event: 

 

Private Sub cmdImportPC_Click()
     DoCmd.RunMacro "mcrImportPC" ' this imports the data from excel power query.  Refresh PQ first! 

End Sub

 

the file location is here (if needed):

C:\Users\Me\Box\Financing\ProjectCostsPQ_FE.xlsx

 

thank you

1 ACCEPTED SOLUTION
jennratten
Super User
Super User

You can create a macro to refresh Power Query and just run it before importing to Access.  Here is some sample code to refresh queries in Excel:

Sub Refresh_All_Data_Connections()

'-------------------------------------------------------------------------------------
'Purpose:   Refresh all data connections in the active workbook
'           after the sub below turns off all background refreshing
'
'Source:    https://stackoverflow.com/questions/22083668/wait-until-activeworkbook-refreshall-finishes-vba
'
'Editor:    Jenn Ratten
'
'Revisions
'01/30/19   Enhanced the speed
'-------------------------------------------------------------------------------------
    
    For Each objConnection In ActiveWorkbook.Connections
        'Get current background-refresh value
        bBackground = objConnection.OLEDBConnection.BackgroundQuery

        'Temporarily disable background-refresh
        objConnection.OLEDBConnection.BackgroundQuery = False

        'Refresh this connection
        objConnection.Refresh

        'Set background-refresh value back to original value
        objConnection.OLEDBConnection.BackgroundQuery = bBackground
    Next

End Sub

Sub ChangeConnectionRefreshModeAndRefreshAll()

'-------------------------------------------------------------------------------------
' Purpose:   Turn off background refreshing for all workbook connections.
'            This prevents hangs and crashes
'
' Source:    https://www.myonlinetraininghub.com/excel-forum/vba-macros/pause-macro-until-power-queries-finished-refreshing
'
' Editor:    Jenn Ratten
'
' Revisions
' 01/30/19   Enhanced the speed
'-------------------------------------------------------------------------------------
    
    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

End Sub

 

 

View solution in original post

2 REPLIES 2
jennratten
Super User
Super User

You can create a macro to refresh Power Query and just run it before importing to Access.  Here is some sample code to refresh queries in Excel:

Sub Refresh_All_Data_Connections()

'-------------------------------------------------------------------------------------
'Purpose:   Refresh all data connections in the active workbook
'           after the sub below turns off all background refreshing
'
'Source:    https://stackoverflow.com/questions/22083668/wait-until-activeworkbook-refreshall-finishes-vba
'
'Editor:    Jenn Ratten
'
'Revisions
'01/30/19   Enhanced the speed
'-------------------------------------------------------------------------------------
    
    For Each objConnection In ActiveWorkbook.Connections
        'Get current background-refresh value
        bBackground = objConnection.OLEDBConnection.BackgroundQuery

        'Temporarily disable background-refresh
        objConnection.OLEDBConnection.BackgroundQuery = False

        'Refresh this connection
        objConnection.Refresh

        'Set background-refresh value back to original value
        objConnection.OLEDBConnection.BackgroundQuery = bBackground
    Next

End Sub

Sub ChangeConnectionRefreshModeAndRefreshAll()

'-------------------------------------------------------------------------------------
' Purpose:   Turn off background refreshing for all workbook connections.
'            This prevents hangs and crashes
'
' Source:    https://www.myonlinetraininghub.com/excel-forum/vba-macros/pause-macro-until-power-queries-finished-refreshing
'
' Editor:    Jenn Ratten
'
' Revisions
' 01/30/19   Enhanced the speed
'-------------------------------------------------------------------------------------
    
    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

End Sub

 

 

Ahh.   Nice.  thank you for the tip!  Much appreciated. 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors