Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4027278
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:04:37+00:00 2026-05-20T11:04:37+00:00

I have a workbook where several of my data feeds pass a parameter back

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:04:38+00:00Added an answer on May 20, 2026 at 11:04 am

    Your mistake is using MS-Query. Code up the database calls using ADODB, and wait for the Execute method of the ADODB.Command object.

    …If that’s what you’re actually doing. There’s a certain amount of guesswork here, but it looks as if the status of the query – not the sheet it’s embedded in – is the information you need.

    This code calls a SQL query asynchronously – it’s conceptually similar to a command object, which is (I think) what you’re actually doing – and the crude ‘sleep’ loop can be replaced by a progress bar, or code for polling flags and calculations elsewhere.

    FYI, the state and status properties of ADO objects can be confusing. In general, zero means close, and 1 means open (for objects that return an open connection or a dataset) and values higher than 1 correspond to waiting, or executing.

    You could, of course, just call the query synchronously.

    I could give you code for ‘DataConnection’ but you’re far better off going to ConnectionStrings.com for that.

    
    Public Function FetchRecordSet(SQL As String, Optional CursorType As CursorTypeEnum = adOpenForwardOnly) As ADODB.Recordset
    On Error Resume Next
    
    Set FetchRecordSet = New ADODB.Recordset
    
    With FetchRecordSet
    
        .CacheSize = 8
        Set .ActiveConnection = DataConnection
        .Open SQL, , CursorType, adLockReadOnly, adCmdText + adAsyncFetch
    
        Do While .State > 1
            Application.StatusBar = "Retrieving data... " 
            Sleep 250
        Loop
    
    End With
    
    Application.StatusBar = False
    
    End Function
    

    [Update]

    I’ve learned something since I posted this answer:

    If you know the name of a command, a rowset-returning function, or a named query in an MS-Access database, don’t bother calling it like this:

    
        SQL = "SELECT * FROM MyQuery"
        .Open SQL, , CursorType, adLockReadOnly, adCmdText + adAsyncFetch
    
    

    Call the command by name, and tell the database engine it’s a named command using the adCmdStoredProc constant:

    
        SQL = "MyQuery"
        .Open SQL, , CursorType, adLockReadOnly, adCmdStoredProc + adAsyncFetch
    
    

    It runs much, much faster.

    Look up the CommandTypeEnum on MSDN, and use whatever works best for you:

    https://msdn.microsoft.com/en-us/library/ms675946(v=vs.85).aspx

    Use adCmdTable for named tables, and see whether that works better than adCmdStoredProc for ‘view’ objects – I’ve found that it varies between database engines.

    [/Update]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data that's currently stored in an Excel workbook. It makes sense
I have a VB application, that uses some global variables to store data that
I have a need to copy a worksheet from one workbook into another and
I have an Excel 2007 workbook that contains an ODBC data connection (to FoxPro,
I have a workbook with several sheets. I have dates on the sheets that
I have several sparsely populated tables in an Excel workbook from which I need
I have an Excel 2007 workbook that contains tables of data that I'm importing
I have a workbook with 20 different pivot tables. Is there any easy way
I have a workbook with two sheets. I would like to format the cell
I have an Excel 2003 workbook that contains a macro to copy certain of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.