MS Access Form按钮,允许用户浏览/选择文件,然后将文件导入表格

时间:2017-05-02 18:38:00

标签: ms-access access-vba

在我的数据库中,我可以使用以下命令按钮导入文件: DoCmd.TransferText acImportDelim,“Import_ Import Specification中的原始数据”,“导入的原始数据”,D:\ Users \ Denise_Griffith \ Documents \ Griffith \ PRIME RECON FILES \ jdaqawmslesfilesemailDLX_SHPREC_2017-04-26_03-33-47.csv,True, “”

但是我想让用户选择要导入的文件,因为文件名每天都会根据创建的日期和时间而不同。我找到了这个站点(http://access.mvps.org/access/api/api0001.htm),并且能够弹出对话框以允许用户导航并选择文件,但我不知道如何合并它,因此所选文件是使用我创建的规范,并进入相应的表。

请帮忙!

1 个答案:

答案 0 :(得分:1)

您需要设置对 Microsoft Office对象库的引用。

Public Sub ImportDocument()
    On Error GoTo ErrProc

    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    With fd
        .InitialFileName = "Some folder"
        .Title = "Some Title"
        With .Filters
            .Clear
            .Add "CSV documents", "*.csv", 1
        End With
        .ButtonName = " Import Selected "
        .AllowMultiSelect = False

        If .Show = 0 Then GoTo Leave
    End With

    Dim selectedItem As Variant
    For Each selectedItem In fd.SelectedItems
        DoCmd.TransferText acImportDelim, "Raw Data from Import_ Import Specification", "Raw Data from Import", selectedItem, True, ""
    Next

Leave:
    Set fd = Nothing
    On Error GoTo 0
Exit Sub

ErrProc:
    MsgBox Err.Description, vbCritical
    Resume Leave
End Sub

在用户评论后更新:

您必须将 Sub 更改为功能并检查返回值。

最简单的方法是返回Boolean,其中FALSE表示已中止,TRUE表示成功。但是,通过这样做,您可以排除错误方案,因为Aborted和Error都将返回FALSE

因此,您可以返回Long值,例如0,1,2分别表示中止,成功和错误。为了避免"魔术数字"但是,我会创建并返回 Enum 类型,如下所示:

Public Enum TaskImportEnum
    Aborted = 0 'default
    Success
    Failure
End Enum

Public Function ImportDocument() As TaskImportEnum
    On Error GoTo ErrProc

    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    With fd
        .InitialFileName = "Some folder"
        .Title = "Dialog Title"
        With .Filters
            .Clear
            .Add "CSV documents", "*.csv", 1
        End With
        .ButtonName = " Import Selected "
        .AllowMultiSelect = False   'Change this to TRUE to enable multi-select

       'If aborted, the Function will return the default value of Aborted
        If .Show = 0 Then GoTo Leave
    End With

    Dim selectedItem As Variant
    For Each selectedItem In fd.SelectedItems
        DoCmd.TransferText acImportDelim, "Raw Data from Import_ Import Specification", "Raw Data from Import", selectedItem, True, ""
    Next selectedItem

   'Return Success
   ImportDocument = TaskImportEnum.Success

Leave:
    Set fd = Nothing
    On Error GoTo 0
    Exit Function

ErrProc:
    MsgBox Err.Description, vbCritical
    ImportDocument = TaskImportEnum.Failure  'Return Failure if error
    Resume Leave
End Function

最后,您可以像这样调用函数:

Sub Import()

    Dim status_ As TaskImportEnum
        status_ = ImportDocument

    Select Case status_
        Case TaskImportEnum.Success:
            MsgBox "Success!"

        Case TaskImportEnum.Failure:
            MsgBox "Failure..."

        Case Else:
            MsgBox "Aborted..."
    End Select

End Sub

您可以在此处详细了解Enum类型:http://www.cpearson.com/excel/Enums.aspx