Excel VBA Wait for Querytable Refresh to Finish

medberg

New Member
Joined
Jul 29, 2011
Messages
30
Hey everyone,
In Excel VB I have a querytable that I use to send update queries to a SQL server. It is embedded in a for loop that sends thousands of queries. Each time through the for loop, the query is constructed, and I use the command
Code:
Sheet3.QueryTables(1).CommandText = varSQL
varSQL is the UPDATE query. After this, I need to call
Code:
Sheet3.QueryTables(1).refresh
to make the update query actually run. Now my issue is I cannot have this query refreshing in the background, so after this refresh I call
Code:
Sheet3.QueryTables(1).cancelrefresh
Sometimes, the refresh is not finished when it cancels, and the update is not actually stored in the database. Is there any way to make Excel wait for the refresh to finish once before canceling the background refresh? I've tried many things, and unfortunately
Code:
.refresh backgroundquery:=true
is not an option. Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try:

Sheet3.QueryTables(1).refresh BackgroundQuery:=False

and get rid of the CancelRefresh.
 
Upvote 0
Thanks for the really quick reply. When I do that, do I include the .refresh? I tried it with and without and got both times "Runtime error '1004': Application-Defined or Object-Defined Error. Thanks.
 
Upvote 0
No, you only need one .Refresh. The whole thing should be:
Code:
Sheet3.QueryTables(1).CommandText = varSQL
Sheet3.QueryTables(1).refresh BackgroundQuery:=False
 
Upvote 0
Thanks again for the reply. Well, my code is weird. I basically check to see if there are any connections, and if there is not, I run If
Code:
ActiveWorkbook.Connections.Count = 0 Then
            With Sheet3.QueryTables.Add(Connection:="ODBC;DSN=" + DSN + ";Trusted_Connection=Yes;APP=2007 Microsoft Office system;DATABASE=" + DATABASE + ";" _
            , Destination:=Range("Sheet3!$A$1"), Sql:=varSQL)
            .rEFRESH BackgroundQuery:=False
            End With
        End If

If there is a connection, I want to just change the query, so I run
Code:
If ActiveWorkbook.Connections.Count > 0 Then
            Sheet3.QueryTables(1).CommandText = varSQL
        End If

Is there a better way to be doing this? The code I have here complies to what you suggested, but I am getting that same 1004 error on the .refresh backgroundquery line. Thanks for any more advice in advance.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,812
Members
452,945
Latest member
Bib195

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top