+ Reply to Thread
Results 1 to 15 of 15

Changing Access Database table in excel

  1. #1
    Forum Contributor
    Join Date
    08-18-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    146

    Changing Access Database table in excel

    I have a MS Access database and linked that to MS Excel. How to change connection together with the database table in excel?

    For example, I change the connection in connection file and the data with new database from Database2018.accdb to Databse2019.accdb, all details are coming from the new database, however when I tried to create a formula the database table is still Database2018.accdb. Is there a way that the database table will also be Database2019.accdb together with the formulas?

    As of now there are only 2 ways I think of: one is to delete the old database table (Database2018.accdb) and created a new one with (Database2019.accdb) and recreate the formulas (which I don't want to do). In which my formula will also be deleted and ending in #REF!. Second is adding the new connection and replacing Database2018.accdb with Databse2019.accdb.

    My sample formula: =(Database2018.accdb[[Month]:[Year]]*12)

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

    Re: Changing Access Database table in excel

    You'll need VBA to change the connection string.

    The way to do it is to take the existing string and SPLIT it with the semicolon delimiter, find the pieces that point to the old database, replace them with the new pointers and then JOIN the parts back.

    Here is an example of code that changes the password in a connection string
    Please Login or Register  to view this content.
    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 Contributor
    Join Date
    08-18-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    146

    Re: Changing Access Database table in excel

    What if I'm using this in our company and only IT or Admin has the password, can I able to use that code?

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

    Re: Changing Access Database table in excel

    The example I gave looks at a connection string for an Oracle ODBC connection. If there is a saved password, it is printed IN THE CLEAR in the connection string.

    I do know what a connection string looks like for Access. It looks something like this:
    DSN=MS Access Database;DBQ=\\TSGVM01484\Common\Labor Analysis\Amazon\Database\Data\Amazon Data.accdb;DefaultDir=\\TSGVM01484\Common\Labor Analysis\Amazon\Database\Data;DriverId=25;FIL=MS

    I don;t see a provision for a password in this connection string. If there is a password, it is managed within the database, most likely after the connection. If you are accessing this data now, then you should be able to access it when you point to it with the connection string.

    It looks like the part you want to change is the DBQ=.

  5. #5
    Forum Contributor
    Join Date
    08-18-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    146

    Re: Changing Access Database table in excel

    I see.

    Yes, that's right. Where do I need to put it in the VB code? And what part of the string do I need to put? Is there specific range? Apologies, I'm not really good in VBA.

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

    Re: Changing Access Database table in excel

    As for the code, put it in a regular module. As for the string to replace, go to the Data Ribbon, Select queries and connections. Click on the query in question. go to the second tab, copy / paste the connection string to a post, and I'll see what I can do with it.

  7. #7
    Forum Contributor
    Join Date
    08-18-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    146

    Re: Changing Access Database table in excel

    Okay, do I need to edit the code? Like I need to put in the new connection string?

    here's the connection string:
    Provider=Microsoft.ACE.OLEDB.12.0;User ID=User;Data Source=c:\\January 2019.accdb;

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

    Re: Changing Access Database table in excel

    This should work. I have a different kind of connection so I can't test the code, but the concept is the same.
    Please Login or Register  to view this content.
    This code is set up to go through ALL the workbook connections and reset them. It will break connections if they are not of this type. If you want to do just one connection, then eliminate the lines of code in red and replace the piece in green with the name of the connection in quotes.

    The code is meant to take parameters for the user ID and the new database. The following shows an example where the User ID is in cell A1 and the new db name is in cell A2.
    Please Login or Register  to view this content.
    Last edited by dflak; 03-12-2019 at 02:34 PM.

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

    Re: Changing Access Database table in excel

    I just noticed an error. I have to make the strings look exactly alike so replace
    Please Login or Register  to view this content.
    With
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    08-18-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    146

    Re: Changing Access Database table in excel

    Thanks dflak, I will try this later and let you know.

  11. #11
    Forum Contributor
    Join Date
    08-18-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    146

    Re: Changing Access Database table in excel

    Apologies for late reply, I already tried the code but there's nothing happened. I also tried adding the source and still it does not refresh to make the new connection. Maybe I'm doing it wrong?

    So here's what I do: I opened the excel with the old connection, add the code, then run it.

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

    Re: Changing Access Database table in excel

    Sometimes the entire connection string is NOT shown in the properties of the connection. There is sometimes a "Prefix" that is not shown.

    Try this in in a module to see what Excel thinks the connection string really is:
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    08-18-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    146

    Re: Changing Access Database table in excel

    I think I know what I did wrong, I tried this and get to see that the code is ODBC, but my string is OLEDB. I replace ODBC with OLEDB in the code you provided, and it works and I get the connection string change! Thank you very much for patience to get this solved! You're awesome!

    Is there also a way to add in the code to change the connection file and connection name? or it is manually changed?

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

    Re: Changing Access Database table in excel

    Quote Originally Posted by dummy777 View Post
    Is there also a way to add in the code to change the connection file and connection name? or it is manually changed?
    Explain to me a bit more. Verbally walk me through the process of how you change the databases.

    I'm just speculating on how you do business. As you can see, with the Test macro in my OP, I read these values from cells in the workbook and pass them as parameters to the routine. The database has a name that can be generated by the current date. So, the cell that contains the database name could be calculated.

  15. #15
    Forum Contributor
    Join Date
    08-18-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    146

    Re: Changing Access Database table in excel

    I'm changing it manually: by deleting the old database and add the new one. By doing so, Connection Name, Connection File and Connection String have been changed to new database. Or to change the connection file by clicking Data>Connections>select which database I will edit>Properties>Definition>then click browse and look for the new database. It changes the Connection file together with connection string then after that I will edit the Connection Name by clicking Data>Connections>select which database I will edit>Properties>edit Connection Name. However if I have multiple database table in my excel, I'm doing it manually and it really takes time just why I'm looking for forum like this and luckily I found the answers.

+ 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. Overwrite access database from excel table using VBA
    By n21crowns in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-05-2018, 03:34 AM
  2. Comparing an MS Access database table columns to an excel spreadsheet
    By 1aaaaaaaaaaaaaaa in forum Tips and Tutorials
    Replies: 0
    Last Post: 05-31-2018, 06:33 PM
  3. Link excel table to access database
    By c00ly81 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-02-2016, 09:56 AM
  4. Access Database and Excel Pivtot Table Issue
    By jefflab1 in forum Excel General
    Replies: 0
    Last Post: 08-11-2015, 11:05 PM
  5. Replies: 2
    Last Post: 07-05-2013, 09:37 AM
  6. Deleting all records in a access database table from Excel
    By Dave31 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-29-2008, 02:18 PM
  7. [SOLVED] Link an excel worksheet to an access database table?
    By w97667 in forum Excel General
    Replies: 1
    Last Post: 04-05-2006, 02:25 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