MS Access Form button that allows user to browse/c

2019-07-24 20:23发布

问题:

In my database, I can made a command button import a file using the following: DoCmd.TransferText acImportDelim, "Raw Data from Import_ Import Specification", "Raw Data from Import", D:\Users\Denise_Griffith\Documents\Griffith\PRIME RECON FILES\jdaqawmslesfilesemailDLX_SHPREC_2017-04-26_03-33-47.csv, True, ""

But I would like to have the user choose the file to import, since the filename is different every day based on date and time it was created. I have found this site (http://access.mvps.org/access/api/api0001.htm) and was able to get the dialog to pop up to allow the user to navigate and select the file, but I do not know how to incorporate it so the file selected is imported using the specification I created, and into the appropriate table.

Please help!

回答1:

You need to set a reference to Microsoft Office Object Library.

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

Update after user's comments:

You must change the Sub to a Function and check the return value.

The simplest way is to return a Boolean, where FALSE indicates aborted and TRUE indicates success. However by doing so, you exclude the Error scenario as both Aborted and Error will return FALSE.

Therefore you can return a Long value e.g. 0, 1, 2 indicating Aborted, Success and Error respectively. In order to avoid the "magic numbers" though, I would create and return an Enum type as shown below:

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

Lastly, you can call the Function like this:

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

You can read more about the Enum type here: http://www.cpearson.com/excel/Enums.aspx