0

I want to browse/select a database file through an Access form and run a query on it based on the file path of the selected database file. I have tried like this:

SELECT *
    FROM ExternalTableName IN '[Forms]![MyForm]![SelectedFilePath]'
    WHERE Condition

...but that didn't work however this SQL did work:

SELECT *
    FROM ExternalTableName IN 'C:\users\desktop\filename.mdb'
    WHERE Condition

For browsing the file, I used this VBA snippet:

Private Sub cmd1()
    Dim fd As FileDialog
    Dim oFD As Variant
    Dim fileName As String

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .ButtonName = "Select"
        .AllowMultiSelect = False
        .Filters.Add "Access Files", "*.mdb", 1
        .Title = "Choose Text File"
        .InitialView = msoFileDialogViewDetails
        .Show

        For Each oFD In .SelectedItems
            fileName = oFD
        Next oFD
        On Error GoTo 0
    End With

    '~~> Change this to the relevant TextBox
    Me.TextFieldName = fileName

    Set fd = Nothing
End Sub
ashleedawg
  • 20,365
  • 9
  • 72
  • 105
  • So you have the answer? examples #2 and #3 worked? (You can run a query on an external database using linked tables, but the data is still going to be processed by your database unless you have the data on a server (ie., SQL Server).... Some application needs to handle the processing,) – ashleedawg Mar 31 '18 at 11:51
  • The 2nd Code worked. But I want it to be more user friendly by attaching the file by file dialogue. – Sandip Basak Mar 31 '18 at 11:53
  • Is it really an `MDB` that you're opening (not an `ACCDB`?) – ashleedawg Mar 31 '18 at 14:12
  • See the code below. – ashleedawg Mar 31 '18 at 14:51

1 Answers1

0

Edit:

To query a table located in an MDB that the user selects from a File Open dialog, the simplest way (while avoiding additional References) is like this:

Option Explicit

Sub testQueryExternalTable()
'displays RecordCount from specified table, selected database
    Const tableName = "tblLog"
    Const defaultPath = "c:\users\" 'default path OR path+filename
    Dim rs As Recordset, fName As String, sql as String

    fName = getFileOpenDialog(defaultPath) 'get filename
    If fName = "" Then MsgBox "You clicked cancel!": Exit Sub

    sql = "select * from " & tableName & " in '" & fName & "'"
    Set rs = CurrentDb.OpenRecordset( sql ) 'query the table
    With rs
        .MoveLast 'count records
        MsgBox .RecordCount & " records found"
        .Close 'close recordset
    End With
    Set rs = Nothing 'always clean up objects when finished
End Sub

Function getFileOpenDialog(defaultPath As String) As String
'returns filename selected from dialog ("" if user Cancels)
    With Application.FileDialog(3)
        .Title = "Please select a database to query" 'set caption
        .InitialFileName = defaultPath 'default path OR path+filename
        .AllowMultiSelect = False 'maximum one selection
        .Filters.Clear 'set file filters for drop down
        .Filters.Add "All Files", "*.*" '(in reverse order)
        .Filters.Add "Access Databases", "*.mdb" '(last = default filter)
        If .Show = True Then getFileOpenDialog = .SelectedItems(1) 'show dialog
    End With
End Function

More Information:


Original Answer:

It's easier (and more efficient) to use Access's built-in functionality rather than recreating it in VBA.

                                                              (Click to enlarge images)
 screenshot   screenshot

The first option imports, and the seconds option emphasized textlinks without importing. Once the table is linked you can work with it in VBA or queries as if it's a local table.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105