+ Reply to Thread
Results 1 to 4 of 4

Connection Commnd text / variable based on lookup table

  1. #1
    Registered User
    Join Date
    08-07-2013
    Location
    Cardiff
    MS-Off Ver
    Office 365
    Posts
    86

    Connection Commnd text / variable based on lookup table

    Hi. I hope someone can help me with this one.

    I currently have a 'Reports Updater' workbook, which is just contains a macro button that when pressed, opens all .xlsm files in a specific folder ("Updating folder"), then does a 'Refresh All' on pivots (Pivot tables linked via a OLEDB connection), saves and closes.

    Works fine, here's the code:


    Please Login or Register  to view this content.

    This is a slightly simplified example. I actually have multiple 'buttons'/macros, that do various things, but the example above is the most frequently used.

    However I'm looking to do something slightly different, and not sure how (or if it's possible).
    I want to open all the workbooks as above, but change each file's connection string 'command text' - based on a reference table in the 'Reports Updater' workbook.

    So for example in the Reports Updater workbook, I'd have a tab called 'connections'. In 'connections' worksheet in column A id have the list of workbook names in 'Updating folder' (eg, "Market Summary - All", "Market Summary - UK" "Market Summary - USA" etc...) , and in Column B I'd have the corresponding connection string command text (eg '"qry_All", "qry_UK", "qry_USA", etc...).

    Any ideas on if/how to do this? I'm fairly sure it can be done, just not sure how to set the variable for the command text based on the table?

    I would be grateful of any help.

    Thanks
    Last edited by Jabba69; 02-03-2018 at 07:57 AM.

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

    Re: Connection Commnd text / variable based on lookup table

    The following wiki addresses MS-Query, however there is a section that explains how to use an Excel Table to manage SQL and write it back to the command text. The SQL statement is contained in two columns in the table. The first column is the "Raw" SQL that contains the "boiler plate" plus some tokens like &START_DATE and &END_DATE where a substitution is needed. The second column in the table does the actual substitution. Most of the time, I am merely substituting a calculated date, but I've also used it to switch tables (one table has end of month data, the other mid-month data - both have the same schema) and have written parts of where clauses such is generating a dynamic list of models in an "IN" statement.

    The substitution technique should work on the command string regardless if it was created my MS-Query. I've not had the opportunity to test this but it's a theory that should work.

    This isn't the best wiki I've ever written, so if you have questions, please ask.

    http://www.utteraccess.com/wiki/MS_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.

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

    Re: Connection Commnd text / variable based on lookup table

    Upon further reading the descriptions, I am almost certain it will work for what you want to do. If you don't need to substitute the command text (I assume it may be the same for all queries) - just look at the SubSQL routine in the included code.

    In fact, this single line should do it for you:
    Please Login or Register  to view this content.
    where xlS is the open workbookand ConnectionName comes from your table.

  4. #4
    Registered User
    Join Date
    08-07-2013
    Location
    Cardiff
    MS-Off Ver
    Office 365
    Posts
    86

    Re: Connection Commnd text / variable based on lookup table

    Thanks for your suggestion.

    I managed to do it in the end as per below:

    Please Login or Register  to view this content.
    Last edited by Jabba69; 02-07-2018 at 06:00 AM.

+ 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. [SOLVED] Variable Lookup in a table
    By markvdh in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-10-2017, 06:30 AM
  2. Change pivot table data connection if current connection = x
    By BellyGas in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-13-2016, 06:05 AM
  3. [SOLVED] table lookup with 2 variable
    By Did13n in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-08-2016, 06:02 AM
  4. Replies: 1
    Last Post: 03-29-2016, 04:29 PM
  5. Passing on Variable path to connection inside query table
    By bharatmvs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-17-2015, 05:11 AM
  6. Multi-Variable Lookup Table
    By k8r0ck in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-12-2014, 05:54 PM
  7. [SOLVED] 2 variable lookup but within a table not column
    By kevinweinberg in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-19-2012, 12:37 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