473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pause while QueryTables import

Uncle Dickie
Hi All,

I have some VB in Excel 2007 which has stopped working since it moved from Excel 2003.

I think the problem is that the QueryTable has not been imported by the time the rest of the code has run but I don't know how to pause!

The variable myProd comes back with 0 and then the For loop does very little!

If I put a break point in the code and then step through a couple of lines it all works fine.

Any pointers would be great:


Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandButton1_Click()
  2.  
  3.     Application.ScreenUpdating = False
  4.     Application.DisplayAlerts = False
  5.  
  6.     Workbooks.Add                       'Create new file for all data
  7.     newname = ActiveWorkbook.Name
  8.     Sheets("Sheet1").Select
  9.  
  10.     With ActiveSheet.QueryTables.Add(Connection:= _
  11.         "ODBC;DRIVER=SQL Server;SERVER=LASSQL01;UID=;APP=Microsoft Office 2003;WSID=PURCHASING5;DATABASE=123_UK;Trusted_Connection=Yes" _
  12.         , Destination:=Range("A1"))
  13.         .CommandText = "exec Lascar.Stock_Profile_Report_Richard"
  14.         .Name = "Query from LascarLive"
  15.         .FieldNames = False
  16.         .RowNumbers = False
  17.         .FillAdjacentFormulas = False
  18.         .PreserveFormatting = True
  19.         .RefreshOnFileOpen = False
  20.         .BackgroundQuery = True
  21.         .RefreshStyle = xlInsertDeleteCells
  22.         .SavePassword = False
  23.         .SaveData = True
  24.         .AdjustColumnWidth = True
  25.         .RefreshPeriod = 0
  26.         .PreserveColumnInfo = True
  27.         .Refresh BackgroundQuery = False
  28. '        .Refresh BackgroundQuery:=False
  29.     End With
  30.  
  31.     myProd = (Application.CountA(Range("A:A")) - 1) / 8      'Number of distinct products to check
  32.  
  33.     For i = 0 To myProd
  34.          Various Formatting Lines
  35.     Next i
  36. End Sub
Jul 11 '11 #1
3 2433
Killer42
8,435 Expert 8TB
I haven't used querytables in Excel, but here are a couple of thoughts.

If you know that it will always return some values, perhaps you could simply loop until myProd comes back non-zero. Something like...
Expand|Select|Wrap|Line Numbers
  1. Do
  2.   myProd = (Application.CountA(Range("A:A")) - 1)
  3. Loop Until myProd
(Any non-zero value will count as True and trigger the Until test)

To do a timed delay, perhaps you can write and call a simple function something like...
Expand|Select|Wrap|Line Numbers
  1. Public Sub Snooze(ByVal NumSeconds As Integer)
  2.   Dim EndTime As Date
  3.   EndTime = DateAdd("s", NumSeconds, Now)
  4.   Do
  5.     DoEvents
  6.   Loop Until Now >= EndTime
  7. End Sub
Then, after the QueryTables.Add, just put in for example Snooze 5 to pause for 5 seconds.
Jul 24 '11 #2
Thanks for the reply.

I have tried your two suggestions as well as a few other variations on the theme and I still have no joy.

I thought it may have been to do with the ScreenUpdating that was disabled but I have tried flicking that on and off during the 'time wasting' loops.

As a final resort I have split the code into 2, the import triggered by one button click and the formatting done once you can see it has loaded (I don't need to worry too much about usability as it is only for my use).

Now the main problem is speed. It used to take about 20s in Excel 2003 but it now takes more than 30minutes in Excel 2007! That's progress!

It seems the conditional formatting is the issue there but I don't think there is much I can do about that as that is what the report is for.
Jul 26 '11 #3
Killer42
8,435 Expert 8TB
Hm...

For starters, yes, from experience I'd say that is Microsoft's idea of progress.

To improve performance, you might try turning off automatic calculation before loading, then hit F9 to calculate afterward. However, I doubt that affects conditional formatting.

It might be worth looking for a way to lose the conditional formatting - perhaps by doing the formatting in your code, so it only has to happen once.

Anyway...

I was just browsing your code again. Remember, I haven't used this feature at all, as I said. But I suspect two different problems here.
  1. ".BackgroundQuery = True" looks as though it's set the opposite way to what you want.
  2. Looking down the end of the code, I also suspect that an unsuccessful attempt to fix the problem is doing the exact opposite of what you intend. If VB is interpreting BackgroundQuery = False as a comparison, then it might return True. Try using ".Refresh False"
It may take a bit of fiddling to work out the interaction between these two parameters.

P.S. To see what I mean about the comparison, go to the Immediate window in the VB editor, and paste in this line...
Expand|Select|Wrap|Line Numbers
  1. Print SomeDumbVariable = False
Jul 27 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: manuel | last post by:
I've two modules: main.py and eval.py Eval.py is imported in main.py, but I've a loop in eval.py that must call a function in main.py... I can't import main.py in eval.py too, because that...
0
by: Andrew | last post by:
When will .NET have a low-pause-time garbage collector A low-pause-time garbage collector would greatly improve .NET's ability to serve as a platform for soft real-time systems. It doesn't have...
11
by: Paminu | last post by:
Is there something like system("PAUSE") for linux?
8
by: Wim | last post by:
My GUI application starts a process (a console program) when the user hits Play. I would like to add an option to pause that process. The code I've added to detect if the user hit pause/unpause...
38
by: Jackie | last post by:
I just want the programme to stop for a while. Thanks.
2
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. Is there a way to start, pause and resume a recurrsive search exactly where you left off, say in the registry programmatically? -- Michael Bragg,...
3
by: DONE1 | last post by:
Hello, I am trying to import 4 separate csv files from 4 different servers into an excel sheet.I want to write a macro which will automate this.Also, i want to append the name of the server for each...
3
by: Basilisk96 | last post by:
Suppose I have a function that may run for a long time - perhaps from several minutes to several hours. An example would be this file processing function: import os def processFiles(startDir):...
3
by: rockmead76 | last post by:
Hi, I'm not a SQL person but have the need to export a tables content (from live) and then import into a test system. A backup and restore is not an option. bcp Database..databasetable out...
0
by: jorgetheawesome | last post by:
Hi! I was writing a program, and for debugging, used the system("pause") command. I am running Windows XP. Its an MS-DOS app, programmed in Dev-C++. But the program, when compiled, runs...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.