+ Reply to Thread
Results 1 to 19 of 19

Macro to Wait for Query to Run

  1. #1
    Registered User
    Join Date
    02-02-2010
    Location
    Indianapolis, Indiana, USA
    MS-Off Ver
    Office Pro 2007 at Work - Office 2010 Beta at Home
    Posts
    23

    Macro to Wait for Query to Run

    Hello,

    I've created an Excel workbook with a couple of different worksheets. One worksheet allows a user to provide a date range by selecting a begin date and end date from a Microsoft Date and Time Picker 6.0 (SP4). There is one control for the begin date and one for the end date. On this same worksheet there is a button named "Run" that has a macro attached to it that executes the query then switched to another sheet which I will call "search results."

    Rather than executing the query and switching immediately to the search results worksheet, I would like for a message to be displayed that says that the query is running then switch to the search results page once the query has finished running.

    When a user would run a query from this workbook, I don't want them sitting there wondering why the search results worksheet is blank so that is why I would like the macro to know when the query is done executing. Any ideas on how I can do this?

    Keep in mind that I do everything codeless, but if you have a suggestion that uses code, I can try to decipher it.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Macro to Wait for Query to Run

    This requires code. I have done this using a userform that shows a "Processing..." message. I have also used progress bars but that is only really helpful for long waits. Another approach is to create a graphic on the page with a "busy" message that is normally not visible, but is made visible by the VBA code and then hidden again when done.

    To do this with a userform, you would create a modeless form (if you allow it to be modal then nothing will execute while it's showing) giving whatever message you want to give. Let's suppose we call the form userformBusyMessage. At the beginning of the macro run by the button you would add the line

    Please Login or Register  to view this content.
    then at the end of the macro, just before switching to the other page, you would use

    Please Login or Register  to view this content.



    P.S. This doesn't sound codeless: ...a button named "Run" that has a macro attached to it that executes the query then switched to another sheet which I will call "search results."
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    02-02-2010
    Location
    Indianapolis, Indiana, USA
    MS-Off Ver
    Office Pro 2007 at Work - Office 2010 Beta at Home
    Posts
    23

    Re: Macro to Wait for Query to Run

    Could you please explain modeless forms to me? I am not familiar with this. I'm very new to macros and any kind of advanced functionality.

    Also, it is not codeless - it is just codeless development since I do not have programming experience.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Macro to Wait for Query to Run

    Quote Originally Posted by SP Brian View Post
    Could you please explain modeless forms to me? I am not familiar with this. I'm very new to macros and any kind of advanced functionality.

    Also, it is not codeless - it is just codeless development since I do not have programming experience.
    This might be a little intimidating if you don't really want to get into coding. If this sounds like too much, try the graphic approach (I got that from someone else, I'll have to look it up to give proper credit).

    Even simpler, you can even designate a cell to contain your message and then populate that cell from code then clear it when the job it done. To get a little fancier you could create a new worksheet, display the message, then delete the worksheet.

    If you want to delve into UserForms, you can bring up the VBA development window with ALT-F11. On the left side, you will see a folder structure, including a folder for the workbook you currently have open. If you right-click on that folder, you can select Insert, and then select UserForm from the submenu. This will create a sub-item for UserForm1 in the folder structure. You can select it, and then press F4 to get a list of its attributes. The first attribute is the name, which is what you should change first to something meaningful. Farther down is an attribute called ShowModal. If it is TRUE (default), then the form will be modal. That means that Excel will not allow the user to select anything outside that form nor allow any code to run as long as the form is being shown. If you set it to FALSE, the form becomes modeless, which means that while the form is being shown, code continues to run and the user can select cells, do menu commands, etc. (Modal and modeless are programming terms that can apply to all windows in the Windows environment.)

    If you double-click on UserForm1, you will get a graphical editor that allows you to define what the form will look like. In your case you just need to go to the toolbox and select Label, drop one on the form, then edit it to your text ("Busy..." or whatever.)

    Once you done all that you can add the code I provided above. I named my form userformBusyMessage.

    This is not really that hard but is a bit of a bump to get over the first time you do it.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Wait for Query to Run

    Hello SP Brian,

    I am guessing you are have set up an External Query. This will hide the Query properties and events from VBA. If the Query is setup in VBA then you will access to the AfterRefresh event. This event signals the end of the refresh and the macro could then be run. The way it is now you will never truly know when the refresh has completed.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: Macro to Wait for Query to Run

    I would have your code alter the caption of the button to something like "Processing...", change the pointer to an hourglass, then run the query but specifying Backgroundquery:= False so that the code only proceeds when the query has run. Finally, reset the cursor and the button caption.
    We'd need the workbook to provide specific code, or at least need to know more about the button.
    You can also trap the AfterRefresh event as Leith suggested, but it takes a little more effort.
    Remember what the dormouse said
    Feed your head

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Macro to Wait for Query to Run

    Quote Originally Posted by SP Brian View Post
    ...executing the query...
    After reading subsequent posts I am now curious--what do you mean by "query"? I am not experienced with using database tables and queries within Excel so maybe my answer is not the most appropriate one.

  8. #8
    Registered User
    Join Date
    02-02-2010
    Location
    Indianapolis, Indiana, USA
    MS-Off Ver
    Office Pro 2007 at Work - Office 2010 Beta at Home
    Posts
    23

    Re: Macro to Wait for Query to Run

    Quote Originally Posted by 6StringJazzer View Post
    After reading subsequent posts I am now curious--what do you mean by "query"? I am not experienced with using database tables and queries within Excel so maybe my answer is not the most appropriate one.
    I have created a query within a system that we use at work. This pulls particular attributes and returns rows of results in the browser. Instead of using a browser, I created a data connection within Excel using the specific URL that allows you to execute the query. So, essential I have a query that has a specific URL that allows for user input in the brackets. This is really difficult to explain because the query is created within an internal system that works with Excel.

    I have 3 sheets in my Excel file. One is the "search parameters" page which allows users to select a begin date from DTPicker1 and an end date from DTPicker2 and another parameter from a Combo Box control. I have a button named "Run" which executes a macro which refreshes the data connection and switches to a new sheet which displays the results of the query.

    I think what I am looking for is for the macro to only switch to the search results page after the refresh has completed.

    Can someone explain how I can tell the macro to only switch to the search results sheet after the refresh has completed / executed?

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: Macro to Wait for Query to Run

    Quote Originally Posted by SP Brian View Post
    Can someone explain how I can tell the macro to only switch to the search results sheet after the refresh has completed / executed?
    Isn't that what we tried to do?

  10. #10
    Registered User
    Join Date
    02-02-2010
    Location
    Indianapolis, Indiana, USA
    MS-Off Ver
    Office Pro 2007 at Work - Office 2010 Beta at Home
    Posts
    23

    Re: Macro to Wait for Query to Run

    I'm a noob because I don't know code. I know that it will take the AfterRefresh event, but that's about all I know.

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: Macro to Wait for Query to Run

    Here's a hint: post your code and it will be much easier for someone to adapt it!

  12. #12
    Registered User
    Join Date
    02-02-2010
    Location
    Indianapolis, Indiana, USA
    MS-Off Ver
    Office Pro 2007 at Work - Office 2010 Beta at Home
    Posts
    23

    Re: Macro to Wait for Query to Run

    I don't know if I'm posting the right part of my code, but here goes nothing. I am looking to wait to switch to the "Search Results" sheet until the refresh has completely executed. I would also like to implement changing the wording on the button from "Run" to something like "Executing..." and the cursor a loading cursur then back after the refresh has executed.

    Please let me know if you have any questions.

    Please Login or Register  to view this content.

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: Macro to Wait for Query to Run

    On the sheet with the query output, right-click the data range, and uncheck the 'Allow background query' option. Your code should then be fine.

  14. #14
    Registered User
    Join Date
    02-02-2010
    Location
    Indianapolis, Indiana, USA
    MS-Off Ver
    Office Pro 2007 at Work - Office 2010 Beta at Home
    Posts
    23

    Re: Macro to Wait for Query to Run

    Sorry, I am not sure where you are telling me to right-click. Would you provide more details, please?

  15. #15
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: Macro to Wait for Query to Run

    On the sheet where the query output is, right-click one of the outout cells and choose 'Data range properties', then uncheck the background refresh option.

  16. #16
    Registered User
    Join Date
    02-02-2010
    Location
    Indianapolis, Indiana, USA
    MS-Off Ver
    Office Pro 2007 at Work - Office 2010 Beta at Home
    Posts
    23

    Re: Macro to Wait for Query to Run

    That worked for not switching until the refresh has been executed. Thank you!

    How do I modify the button to then switch to say "Executing", change the cursor to a loading cursor, switch to the "Search Results" worksheet only after the refresh has executed, change the cursor back to normal, then change the button back to say "Run". After changing the "Search Results" worksheet, I want the cursor and button to be changed but not leave the current sheet.

    Would you provide some insight on this as well, please?

  17. #17
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: Macro to Wait for Query to Run

    Yep, but I need to know what kind of button it is - from the Forms toolbar or the Control Toolbox?

  18. #18
    Registered User
    Join Date
    02-02-2010
    Location
    Indianapolis, Indiana, USA
    MS-Off Ver
    Office Pro 2007 at Work - Office 2010 Beta at Home
    Posts
    23

    Re: Macro to Wait for Query to Run

    From the forms toolbar --> Well, when I click Insert on the Developer tab, it is Button (Form Control)

  19. #19
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: Macro to Wait for Query to Run

    Something like:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1