I have a workbook where several of my data feeds pass a parameter back into the SQL query depending upon some dropdown menus/user actions. This keeps the workbook trim, improves calculation, etc - keeping all of the item-level detail locally in the workbook just isn't practical.
Some elements of my VBA depend upon evaluations of data that comes from these parameterized queries. Herein arises the problem - VBA doesn't wait for the parameter to be passed back to the query before evaluating everything in the macro.
I'm curious if anyone has any thoughts or advice about best practices for programmatically 'pausing' VBA execution until a feed refreshes. My work around right now is to chunk my VBA into two parts, throw anything that depends on the changed data into a separate function, and use application.ontime to pause for X seconds.
Application.OnTime Now + TimeSerial(0, 0, 10), "Restart"
this is a 90% solution but it's less than ideal. The length of time is arbitrary - on a really slow connection it's insufficiently long and on a fast one it is unnecessarily slow.
Ideally there would be some sort of way to wait until Excel was ready and then continue. Similar to how when using the MS Internet Controls library you can use
Do Until .document.ReadyState = "complete"
to pause execution until IE returns a ready state. Any strategies for a more elegant solution?
edit: per jon below, adding the code and explaining how the SQL query works:
select sts1.studentid, sts1.alphascore as testcycle,
sts2.numscore as lexile, sts3.alphascore as gleq, sts4.numscore as nce
from ps.studenttestscore sts1
join ps.students stu on (sts1.studentid = stu.id)
join ps.studenttestscore sts2 on (sts1.studenttestid = sts2.studenttestid)
join ps.studenttestscore sts3 on (sts1.studenttestid = sts3.studenttestid)
join ps.studenttestscore sts4 on (sts1.studenttestid = sts4.studenttestid)
where (stu.id = ? ) and (sts1.testscoreid = 578) and (sts2.testscoreid = 575)
and (sts3.testscoreid = 577) and (sts4.testscoreid = 576)
the ? is a parameter that passes the relevant student ID - MS query uses a cell value for that parameter. the cell that it looks to just has a lookup based on what student has been selected:
=IFERROR(INDEX(Stu!$B:$F,MATCH(Student!B2,Stu!$F:$F,0),1),999999)
(the iferror just passes an arbitrary number up to prevent nasty dialog boxes from popping up if an improper value somehow gets selected).