1. Welcome to Chandoo.org Forums. Short message for you

    Hi Guest,

    Thanks for joining Chandoo.org forums. We are here to make you awesome in Excel. Before you post your first question, please read this short introduction guide. When posting or responding to questions please remember our values at Chandoo.org are: Humility, Passion, Fun, Awesomeness, Simplicity, Sharing Remember that we have people here for whom English is not there first language and we need to allow for this in our dealings.

    Yours,
    Chandoo
  2. Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  3. When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

MS Access - Take source for Access database based on cell value in excel

Discussion in 'The Lounge' started by ThrottleWorks, Oct 29, 2018.

  1. ThrottleWorks

    ThrottleWorks Excel Ninja

    Messages:
    1,879
    Hi,

    I am using below code to import MS Access database in excel.
    This code is written in Excel file. I have only one table in Access database.

    Is it possible to give cell reference instead of full path in the code.
    I am trying to change below line but not able to do so.

    "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\AA\AA\AA\AA\AA.accdb;Mode=ReadWrite", Destination:=Worksheets("DataBase").Range("A1")).QueryTable

    I am trying to replace ‘=\\AA\AA\AA\AA\AA.accdb’ with ‘MyDataSource’.
    Can anyone please help me in this.

    Code (vb):

    Sub OPENCONNECTION()

        Dim MyDataSource As String
        MyDataSource = Worksheets("Sheet1").Range("B1")
     
        Worksheets("DataBase").Select
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
            "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\AA\AA\AA\AA\AA.accdb;Mode=ReadWrite", Destination:=Worksheets("DataBase").Range("A1")).QueryTable
                 
            .CommandType = xlCmdTable
         
            'Name your table below
           .CommandText = Array("MyDatabase")
         
            'Refresh on open?
           .RefreshOnFileOpen = False
         
            'Refresh rate (in minutes)
           .RefreshPeriod = 20
         
            .Refresh BackgroundQuery:=False
        End With
    End Sub
     
  2. vletm

    vletm Excel Ninja

    Messages:
    4,298
    ThrottleWorks
    Cannot verify myself ... guessing ...
    Have You tried to next?
    Code (vb):

    ' original part
    Source:= "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\AA\AA\AA\AA\AA.accdb;Mode=ReadWrite"
    ' modified part
    Source:= "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0; Data Source" & MyDataSource & ";Mode=ReadWrite"
     


    and take care that the 1st character is = in Your 'MyDataSource'-variable.
    ThrottleWorks likes this.
  3. ThrottleWorks

    ThrottleWorks Excel Ninja

    Messages:
    1,879
    Hi sir,

    Thanks for the help. Changed as advised by you. Have a nice day ahead.

Share This Page