ms访问浏览文件并获取文件名和路径

der*_*rek 5 ms-access vba

我正在使用ms访问,我想添加一个按钮来浏览文件,获取文件的名称及其路径.然后我想将文件路径和文件名存储在2个单独的变量中.我到目前为止的代码是在下面,目前我可以浏览文件并只获取文件的名称.任何人都可以帮我添加我的代码来获取文件路径并将文件名和文件路径存储在单独的变量中.

Private Sub Command7_Click()

Dim f As Object

Set f = Application.FileDialog(3)

f.AllowMultiSelect = True

If f.Show Then
    For i = 1 To f.SelectedItems.Count
        MsgBox Filename(f.SelectedItems(i))
    Next
End If

End Sub


Public Function Filename(ByVal strPath As String) As String

If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
    Filename = Filename(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)

End If

End Function
Run Code Online (Sandbox Code Playgroud)

Fio*_*ala 9

您正在传递函数的完整路径,因此您可以从中获取路径.例如:

Public Function Filename(ByVal strPath As String, sPath) As String
    sPath = Left(strPath, InStrRev(strPath, "\"))
    Filename = Mid(strPath, InStrRev(strPath, "\") + 1)
End Function
Run Code Online (Sandbox Code Playgroud)

叫,说:

    sFile = Filename(f.SelectedItems(i), sPath)
    MsgBox sPath & "---" & sFile
Run Code Online (Sandbox Code Playgroud)

在全

Private Sub Command7_Click()

Dim f As Object

Set f = Application.FileDialog(3)

f.AllowMultiSelect = True

If f.Show Then
    For i = 1 To f.SelectedItems.Count
        sFile = Filename(f.SelectedItems(i), sPath)
        MsgBox sPath & "---" & sFile
    Next
End If

End Sub


Public Function Filename(ByVal strPath As String, sPath) As String
    sPath = Left(strPath, InStrRev(strPath, "\"))
    Filename = Mid(strPath, InStrRev(strPath, "\") + 1)
End Function
Run Code Online (Sandbox Code Playgroud)


Han*_*sUp 6

对于您想要从单击事件过程中获得的内容,无需调用单独的自定义 VBA 函数。

Private Sub Command7_Click()
    Dim f As Object
    Dim strFile As String
    Dim strFolder As String
    Dim varItem As Variant

    Set f = Application.FileDialog(3)
    f.AllowMultiSelect = True
    If f.Show Then
        For Each varItem In f.SelectedItems
            strFile = Dir(varItem)
            strFolder = Left(varItem, Len(varItem) - Len(strFile))
            MsgBox "Folder: " & strFolder & vbCrLf & _
                "File: " & strFile
        Next
    End If
    Set f = Nothing
End Sub
Run Code Online (Sandbox Code Playgroud)