+ Reply to Thread
Results 1 to 5 of 5

VBA - Retry SQL Query if Query Fails

  1. #1
    Registered User
    Join Date
    09-25-2020
    Location
    Louisville
    MS-Off Ver
    365 Pro
    Posts
    29

    VBA - Retry SQL Query if Query Fails

    I have multiple workbooks for my organization that are connected to an external data source via SQL query. Every day, I go in and refresh these workbooks. To try and alleviate some of the manual process behind this, I created a simple VBA sub that takes a list of workbook paths from cells on a sheet, opens those workbooks, refreshes all, saves, and moves on to the next one on the list. Here's part of the code:

    Please Login or Register  to view this content.
    The RefresherWB, which contains the list and code, has a field that is updated with success/fail if something were to happen to cause it to fail. However, there are times where for whatever reason (our data warehouse sucks) the SQL query will not complete successfully. Currently the RefresherWB goes through its commands normally and gives it a "success" because obviously nothing went wrong from the VBA perspective.

    Does anyone know if there is a way to pull some kind of error code directly from the SQL query fail? That is, I would either like to retry the refresh all command on the current workbook being refreshed if the SQL query fails, or I would at least like to be able to put an indicator in my RefresherWB that the query in that workbook failed. As it is now, I still have to manually open each workbook to verify the data source updated successfully.



    Thanks in advance.

  2. #2
    Valued Forum Contributor PaulSP8's Avatar
    Join Date
    11-21-2017
    Location
    Chorley, England
    MS-Off Ver
    2013 & 2021
    Posts
    437

    Re: VBA - Retry SQL Query if Query Fails

    I've got kind of the same problem (data warehouse related as well).

    In my case I have Date field in the returned results from the query - To check it's worked OK I check for the latest date in that field, it should include yesterdays date - If it doesn't then I know it's not worked for whatever reason.

    Is there something in your data that would indicate if it had worked or not?

    Actually - While I'm thiking on it - I have another that doesn't have a date field, in that case I check the number of rows in the data before and after for an indication of it's worked or not.

    HTH
    Last edited by AliGW; 02-18-2022 at 08:23 AM. Reason: PLEASE don't quote unnecessarily!

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: VBA - Retry SQL Query if Query Fails

    Check the properties on the queries in the workbooks. Make sure "Allow background query" is checked OFF. It is normally checked on by default when you create the query.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  4. #4
    Registered User
    Join Date
    09-25-2020
    Location
    Louisville
    MS-Off Ver
    365 Pro
    Posts
    29

    Re: VBA - Retry SQL Query if Query Fails

    That's a decent idea, I could check the number of rows before and after and if they match, retry the refresh until the numbers differ or it reaches some limiting number of attempts. I'll give that a try. Thanks!

  5. #5
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: VBA - Retry SQL Query if Query Fails

    Here is a concept. I use this when copy / pasting ranges as pictures in VBA. In one of my applications, it worked about 95% of the time, so a single retry is likely to succeed.

    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)

Similar Threads

  1. VBA to Update Several Query Tables from Web Page Fails
    By chasoe in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-25-2020, 07:43 AM
  2. Excel Power Query Refresh or Access Query - 2nd Query Run is faster
    By Steveapa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-03-2020, 10:16 AM
  3. Web query fails after five minutes
    By ptrckbrntt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-25-2016, 10:27 PM
  4. Connecting to ODBC using ADO Fails at Query
    By Shermer321 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-12-2013, 01:00 PM
  5. [SOLVED] Excel VBA database query fails using ADO.
    By gmahlert in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-19-2012, 12:15 PM
  6. [SOLVED] Excel Query fails to include data?
    By Office 2K Issues in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2005, 09:15 PM
  7. [SOLVED] Web query fails to preserve html formatting
    By Bob in forum Excel General
    Replies: 0
    Last Post: 07-07-2005, 12:05 PM

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