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 8231199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:24:19+00:00 2026-06-07T17:24:19+00:00

This seems to be a fairly common problem but none of the the solutions

  • 0

This seems to be a fairly common problem but none of the the solutions I’ve found seem to work.

I’m grabbing some data from SQL Server and copying it into a worksheet. Then I want to copy a range from the new data and do other stuff with it. All of this happens in a single vba function.

My problem is when the function is run from Excel it moves onto the second part of the function without waiting for the query to return the required data.

Of course the function works fine when I run it from the vba IDE.

Dim a As New ADODB.Connection
Dim r As New ADODB.Recordset

a.Open (connStr)
Set r = a.Execute(sqlstr)

sht.Range("A2").CopyFromRecordset r

'please wait here until the proc has executed?

checkData = sht.Range("A2").Value

When I run the function from Excel checkData is always empty, when I run it with F5 it always has the required data.

  • 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-06-07T17:24:24+00:00Added an answer on June 7, 2026 at 5:24 pm

    Does this work?

    Dim a As New ADODB.Connection
    Dim r As New ADODB.Recordset
    
    a.Open (connStr)
    Set r = a.Execute(sqlstr)
    
    Do
      'Wait
    Loop Until Not r Is Nothing
    
    sht.Range("A2").CopyFromRecordset r
    
    checkData = sht.Range("A2").Value
    

    Alternatively, if this fails, you could try testing some property of r, like EOF or BOF, and if an error occurs, or you get an unexpected value you know the data hasn’t yet loaded. For example:

    Dim a As New ADODB.Connection
    Dim r As New ADODB.Recordset
    
    a.Open (connStr)
    Set r = a.Execute(sqlstr)
    
    On Error Resume Next
    Do
      Err.Clear
      r.EOF 'Put your test here, you might test rowcount or similar.
            'I've simply asked for the EOF property and ignored the result, I'm 
            'not sure if this will work in your case. Some testing may be required.
    While Err.Num <> 0
    On Error GoTo 0 'Or whatever you previously had this set to
    
    sht.Range("A2").CopyFromRecordset r
    
    checkData = sht.Range("A2").Value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems to be a fairly common problem, but the solutions that I have
Arg, looks like this is a fairly common problem but I can't seem to
It seems this is a common question/problem but despite checking out a number of
I have what I think is a fairly common problem, but I cannot seem
I got a problem which seems to be fairly common but i really don't
This seems like another fairly simple thing to do, but I'm again struggling on
This seems like a fairly obvious question, but I haven't been able to think
This seems like it would be fairly simple, but I've been unable to find
This seems to make perfect sense, but it is not working. I seem to
This seems to be a fairly common thing to do, and I've managed to

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.