Consulting

Results 1 to 2 of 2

Thread: Imprt data from MDB file

  1. #1
    VBAX Newbie
    Joined
    Jan 2020
    Posts
    2
    Location

    Imprt data from MDB file

    I have some code to import from a MDB file. I want to make it variable, i.e. use a excel cell to set the path of the MDB file. I cannot figure out how to do this.

    My code:

    Sub MDB_DATA()
    ' MDB_DATA Macro
    ActiveWorkbook.Queries.Add name:="tblStructures", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Access.Database(File.Contents(""C:\Structures.mdb""), [CreateNavigationProperties=true])," & Chr(13) & "" & Chr(10) & "    _tblStructures = Source{[Schema="""",Item=""tblStructures""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Removed Other Columns"" = Table.SelectColumns(_tblStructures,{""FileName"", ""LiveLoadInc""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Removed Other Columns"""
        ActiveWorkbook.Worksheets.Add
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=tblStructures;Extended Properties=""""" _
            , Destination:=Range("$A$1")).QueryTable
            .CommandType = xlCmdSql
            .CommandText = Array("SELECT * FROM [tblStructures]")
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "tblStructures"
            .Refresh BackgroundQuery:=False
        End With
    End Sub
    Last edited by Aussiebear; 11-09-2021 at 02:23 PM. Reason: Added code tags to supplied code

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,158
    Location
    Hi there,

    Maybe something like the below:

    Sub MDB_DATA()
    ' MDB_DATA Macro
    
    
    Dim fPath As String
    fPath = "C:\Users\NoName\Desktop\test.mdb"
    
    
    ActiveWorkbook.Queries.Add Name:="tblStructures", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Access.Database(File.Contents(""" & fPath & """), [CreateNavigationProperties=true])," & Chr(13) & "" & Chr(10) & "    _tblStructures = Source{[Schema="""",Item=""tblStructures""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Removed Other Columns"" = Table.SelectColumns(_tblStructures,{""FileName"", ""LiveLoadInc""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Removed Other Columns"""
        ActiveWorkbook.Worksheets.Add
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=tblStructures;Extended Properties=""""" _
            , Destination:=Range("$A$1")).QueryTable
            .CommandType = xlCmdSql
            .CommandText = Array("SELECT * FROM [tblStructures]")
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "tblStructures"
            .Refresh BackgroundQuery:=False
        End With
    End Sub
    Just refer fPath to a cell value than contains a path, like the below:

    fPath = Range("A1").value
    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2401, Build 17231.20084

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •