+ Reply to Thread
Results 1 to 5 of 5

Excel vba to populate more than one sheet from a sql database

  1. #1
    Registered User
    Join Date
    11-11-2021
    Location
    New Zealand
    MS-Off Ver
    Office 365
    Posts
    37

    Excel vba to populate more than one sheet from a sql database

    I am not experienced in excel vba. I have an excel workbook with more than 1 sheet. Each sheet should refresh on clicking a button within the sheet. Providing here the code used with just 2 sheets as an example and have removed all sensitive data from the code. I need help with the following:

    a. if I give this document to someone else they are prompted for a database name and password? Since there is an EXEC function used I am forced to use the Read-Write authentication which I cannot distribute to every user. How can I embed the connection string here so other users are not prompted for database login details.

    I am not sure how can I provide an Excel file as an example without revealing any sensitive data. However the whole VBA code is copied here after deleting sensitive data.

    Please Login or Register  to view this content.

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

    Re: Excel vba to populate more than one sheet from a sql database

    Have you investigated MS-Query? It is ancient technology but it still works.

    If you set up the query, you will have the connection string and the command string available to you.

    To change the credentials put the connection string into an array splitting on the semi-colon. Change those members of the array that contain the credentials and then remerge the array members back into a string using the semicolon.

    Unfortunately, I can't find a code stample where I did this.

    The main issue is that the connection string contains the credential information IN THE CLEAR. The best you can do is to put a password on the VBA project. Also when the application runs, it will run the query under the credentials provided by the code so people will be running the query as if they were you.

    If you decide to go the MS-Query route, let me know if you have any questions, I'm pretty good with it and have another application that allows you to change the command string ON THE FLY using Excel Tables.
    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
    Registered User
    Join Date
    11-11-2021
    Location
    New Zealand
    MS-Off Ver
    Office 365
    Posts
    37

    Re: Excel vba to populate more than one sheet from a sql database

    Thanks dflak. If i decide to go down the path of MS-Query i have the following questions:
    1.I have a stored procedure as a query: EXEC TATDataReport 'AUCKLAND',2,2024.
    a. I would like to add these inputs in the spreadsheet as a dropdown or user entry and the user can then select/input the options.
    b. So EXEC TATDataReport 'AUCKLAND',2,2024 would read: EXEC TATDataReport [sLoc],[mthNum],[yearNum] - How do make this work?

    2.My workbook has 4 worksheets - one for each location. So can i add a button to the sheet to retrieve data based on the inputs?

    Regards docgee

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

    Re: Excel vba to populate more than one sheet from a sql database

    OK, here is a piece of code I do have.

    This application allows you to rewrite parts of the SQL code at runtime. So I would set up a table with SLoc, mthNum and yearNum as the "tokens" as described in the word document. The example in the documents and the example spreadsheet reads the data to be substituted in from named ranges.

    Here is an idea for calling the SubSQL subroutine

    Please Login or Register  to view this content.
    Unzip the attached for more details.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    11-11-2021
    Location
    New Zealand
    MS-Off Ver
    Office 365
    Posts
    37

    Re: Excel vba to populate more than one sheet from a sql database

    Hi
    I have found a different way to work this out but am stuck at one spot in the vba code. I am not sure how to write Redim Preserved array for the following output.


    Please Login or Register  to view this content.
    I have checked and the resultsArray is correct. I am getting a "Run-time error '9' Subscript out of range" on the Redim Preserve line below

    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. [SOLVED] Populate Sheet from Database
    By AllisterB in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 01-14-2021, 05:32 PM
  2. Auto populate quote sheet with values from a database
    By Matcho09 in forum Excel General
    Replies: 1
    Last Post: 07-09-2019, 09:02 AM
  3. [SOLVED] populate excel userform from access database
    By Megatronixs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-16-2015, 03:19 PM
  4. Populate invoice from database sheet (Attached)
    By Mayweed in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-24-2010, 08:55 AM
  5. Populate Excel report from Database
    By haidermirza in forum Excel General
    Replies: 3
    Last Post: 03-27-2010, 03:40 PM
  6. How to populate excel combo box with access database?
    By computerguy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-04-2006, 11:05 PM

Tags for this Thread

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