【问题标题】:Get Access DB filename with FileOpenDialog then run query against table within it?使用 FileOpenDialog 获取 Access DB 文件名,然后对其中的表运行查询?
【发布时间】:2018-03-31 11:46:34
【问题描述】:

我想通过 Access 表单浏览/选择一个数据库文件,并根据所选数据库文件的文件路径对其运行查询。我试过这样:

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

...但这不起作用但是这个 SQL 确实起作用了:

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

为了浏览文件,我使用了这个 VBA sn-p:

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

【问题讨论】:

  • 那么你有答案了吗?示例 #2 和 #3 有效吗? (您可以使用链接表在外部数据库上运行查询,但除非您在服务器(即 SQL Server)上有数据,否则数据仍将由您的数据库处理......某些应用程序需要处理处理,)
  • 第二个代码有效。但我希望通过逐个文件对话框附加文件来使其更加用户友好。
  • 您打开的真的是MDB(不是ACCDB?)
  • 见下面的代码。

标签: ms-access vba ms-access-2010 fileopendialog


【解决方案1】:

编辑:

要查询用户从文件打开对话框中选择的MDB中的表,最简单的方法(同时避免额外的引用)是这样的:

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

更多信息:


原答案:

使用 Access 的内置功能比在 VBA 中重新创建更容易(也更有效)。

(单击放大图像)
  

第一个选项导入,第二个选项强调文本链接而不导入。链接表后,您可以在 VBA 中使用它或像本地表一样进行查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多