+ Reply to Thread
Results 1 to 19 of 19

Refresh SQL data link by macro instead of excels connection wizard

  1. #1
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Refresh SQL data link by macro instead of excels connection wizard

    Hi,

    I have finally got round to linking all my spreadsheets up to our oracle database through an access SQL query, one problem I have with one workbook is that the on open macro completes before the workbook updates its SQL link.

    The SQL is set up through excels normal external data link wizard and runs when you open the workbook. I need a way to trigger the SQL refresh from the macro below and make sure the macro doesn't continue until the SQL refresh is complete.

    Code for this workbook is below, the SQL link is on the workbook called "UPDATER", the workbook called "MASTER SAR" is the file that is ultimately updated. For security and database connection issues I cannot have the SQL query on the "MASTER SAR" workbook.

    Workbook

    Please Login or Register  to view this content.
    Module 1

    Please Login or Register  to view this content.
    Module 2

    Please Login or Register  to view this content.
    Module 3

    Please Login or Register  to view this content.
    Last edited by mcinnes01; 11-17-2010 at 10:42 AM.

  2. #2
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    Office 2019 Enterprise
    Posts
    473

    Re: Refresh SQL data link by macro instead of excels connection wizard

    Not looked fully at your code but I had this same problem. I had a macro that refreshed a query and then emailed it but the email was created before the query refreshed. The only way I've found around it is to pause the macro for x seconds using this code

    Please Login or Register  to view this content.
    and if you wanted to pause for 5 seconds then you would put this in your code

    Please Login or Register  to view this content.
    Maybe theres a way around it but Im not aware of it.

    John

  3. #3
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: Refresh SQL data link by macro instead of excels connection wizard

    Thanks for that, I have been hunting around and haven't got anything working yet but it seems I may be able to use an afterrefresh command.

    My only issue is im not sure about qt querytables as an object and my code errors on that.

    Also I haven't ever used a class module and I'm not sure why I would need to use one or what they are for:

    This is what I have so far it will call module2.savesrv once the sql update has success....

    The code errors on module4 on line

    Please Login or Register  to view this content.
    Workbook

    Please Login or Register  to view this content.
    Module 4

    Please Login or Register  to view this content.
    Class 1

    Please Login or Register  to view this content.
    Last edited by mcinnes01; 11-17-2010 at 12:30 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    02-08-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2007
    Posts
    122

    Re: Refresh SQL data link by macro instead of excels connection wizard

    Search for the Enable background refresh property of your connections and be sure that is unchecked.

    In Excel 2007 is in:

    Data->Connections->
    Select your connections
    Properties

    Regards

  5. #5
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: Refresh SQL data link by macro instead of excels connection wizard

    Hi, thanks I have already done that, I think the issue is that I am refering to a query table,

    1 i am not sure what a query table is as I have never used one ( unless it is the thing all that is created when the external data link is refreshed)

    2 If this is the thing that is created when I refresh the external data link then I have no idea why I can't refer to in vba.

    It seems like the Querytables(1) doesn't exist?

  6. #6
    Valued Forum Contributor
    Join Date
    02-08-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2007
    Posts
    122

    Re: Refresh SQL data link by macro instead of excels connection wizard

    Quote Originally Posted by mcinnes01 View Post
    Hi, thanks I have already done that
    If the Background is set correctly this may work.

    Please Login or Register  to view this content.
    Regards

  7. #7
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: Refresh SQL data link by macro instead of excels connection wizard

    That seems to work as far as triggering the refresh from a macro which is great! Do you know how I can now do a module that does the afterrefresh and does this need a class module? I read up a little on class modules and more or less understand the purpose of them, but I am struggling applying it as I haven't used one before.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Refresh SQL data link by macro instead of excels connection wizard

    You won't need any class module.

    Assuming a querytable in A1
    After refreshing cell A1 will have some value.
    So you can use:

    Please Login or Register  to view this content.



  9. #9
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: Refresh SQL data link by macro instead of excels connection wizard

    how do I find out what the query table is called?

    Is the query table the tabale the data comes in from the external source?

    I tried this, a slight adaptation of what you said and I got the error: compile error: wrong number of arguments or invalid property assignment

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Refresh SQL data link by macro instead of excels connection wizard

    I'm not a clairvoyant. It's hard debugging non published code. Please post your 'slightly' adapted code, preferably in a real workbook.

  11. #11
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: Refresh SQL data link by macro instead of excels connection wizard

    Ok this works on my PC but if I try to run it on another PC it errors on line:

    Please Login or Register  to view this content.
    Workbook

    Please Login or Register  to view this content.
    Module4

    Please Login or Register  to view this content.
    Module2

    Please Login or Register  to view this content.
    Module3

    Please Login or Register  to view this content.
    Module1

    Please Login or Register  to view this content.

    I made sure I save both workbooks on a central server first and edit the file paths to relate to that location e.g. X:\payroll\andy\development\sar master.xls etc so I would have thought this would have worked. Also your ODBC connections are saved on your C:\ in my data connections, is there a way of changing this for this worksheet to look at a central server folder instead?

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Refresh SQL data link by macro instead of excels connection wizard

    Why so complicated ?

    Please Login or Register  to view this content.
    If it runs fine on your PC I think it proves the code is correct.
    Use all the debugging tools (F8, window direct, local variables) to see what's wrong on the other computer.

  13. #13
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: Refresh SQL data link by macro instead of excels connection wizard

    I have it working on the server now, but I have had to change the code so it doesn't call the updater workbook by its name, but by activework. I dont know if this is necessarily the best way of refering to things, but it seems to work:

    example

    Please Login or Register  to view this content.
    My other issue is on the closeroutine it doesn't completely close excel just the workbook. I have tried the application.quit, but this results in an error (0) and it gets stuck in an error loop requiring a ctrl alt delete on excel.

    Any ideas?

    Please Login or Register  to view this content.

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Refresh SQL data link by macro instead of excels connection wizard

    You'd better not use Activeworkbook but a correct reference to the workbook:

    Sub SaveSrv()
    -------
    .Value=Workbooks("UPDATER.xls").Worksheets("STAFF").Range("A2:I65536")
    --------
    End Sub

  15. #15
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: Refresh SQL data link by macro instead of excels connection wizard

    Thanks snb, I will make that change when I get back on the server later.

  16. #16
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: Refresh SQL data link by macro instead of excels connection wizard

    any ideas to deal with the application.quit?

  17. #17
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Refresh SQL data link by macro instead of excels connection wizard

    Why do you want to close Excel ?
    I think this is 'over the top' as a separate Sub ( it does the same as yours)

    Please Login or Register  to view this content.

  18. #18
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: Refresh SQL data link by macro instead of excels connection wizard

    i find it easier to think of each part of the code as a separate module at first, then put them all together. I want to completely close excel as this workbook is just an updater the other workbook,

    it is opened by a scheduler then the data on the sheet updates via the sql query, the other workbook is then opened, the data is copied over, then that work book is saved and closed. The new file is then emailed to an inbox where it can be uploaded to the intranet (some drupal watcher import module to made for this) and then the update workbook is saved and excel is completely closed until the next time the scheduler runs.

    Will the .close True work to close excel entirely?
    Last edited by mcinnes01; 11-18-2010 at 12:12 PM.

  19. #19
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: Refresh SQL data link by macro instead of excels connection wizard

    I have tried a few various methods to quit the application, but it seems to create an error (0) then resume (20) i think error.

    I have use application.quit an api method can't remember of the top of my head, but they all have the same problem. One thing I will say is that this is running on a windows 2003 server and I have a feeling the scheduler opens excel then opens the updater workbook from inside that, so if I just use .close there is a book1 left open in the background. The problem with this is that the schedule is left running as the process needs teminating.

    I think I rememeber faguely the other method i tried which was something like kill/f/mi "excel.exe" im not sure, but it didn't work, it did close excel but it created a backup file in the wrong format and saved it in the folder where the workbook is saved. The backup file was of the SAR spreadsheet named with a long number like 700008872, any ideas?
    Last edited by mcinnes01; 11-23-2010 at 11:14 AM.

+ 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