Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50321

Refresh MS Query using VBA

$
0
0
Hi,

I am having a problem with refreshing an MS Query that drives the sheet I am working on, the MS Query seems to run, the data does not refresh.

Normally I would open the work book and go to the data tab and press the "Refresh All" button, then in the lower task bar on the right i get "Waiting for query to execute" after a few seconds on the right hand side i get a spinning globe and the message "Running Background Query" and after a few more seconds it finished and it works fine, so I recorded a macro of this and put it into a loop,

the code I have tried is as follows:

Code:

Do While MyFile <> ""
           
        Workbooks.Open (MyFile)
       
        With Worksheets("Financial Analysis")
          ActiveWorkbook.RefreshAll
          ActiveWorkbook.Close True
        'End With

        Application.DisplayAlerts = 0

        MyFile = Dir()
    Loop

But this opens the workbooks, refreshes all, then in the lower task bar on the right i get "Waiting for query to execute" after a few seconds the work book saves and closes and the data is not refreshed.

So I tried:

Code:

Do While MyFile <> ""
           
        Workbooks.Open (MyFile)

        With ActiveWorkbook.Connections("extractcodequery").ODBCConnection
                .BackgroundQuery = True
                .Refresh
        End With
       
        With ActiveWorkbook.Connections("extractcodequery2").ODBCConnection
                .BackgroundQuery = True
                .Refresh
            ActiveWorkbook.Close True
        End With

        Application.DisplayAlerts = 0

        MyFile = Dir()
    Loop

But the same thing happens, the MS Query runs, but when the "Running Background Query" fails to happen and the workbook saves, closes and there is no new data!

Can anyone help me out?

thanks

Viewing all articles
Browse latest Browse all 50321

Latest Images

Trending Articles



Latest Images