ms access浏览文件并获取文件名和路径 [英] ms access browse for file and get file name and path

查看:499
本文介绍了ms access浏览文件并获取文件名和路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using ms access and i want to add a button to browse for a file, get the name of the file and its path . i then want to store the file path and file name in 2 separate variables. The code i have so far is below and at the moment i can browse for a file and get the name of the file only. Can anyone help me add to my code to get the file path and to store both the file name and file path in separate variables.

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

推荐答案

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

You are passing the full path to your function, so you can get the path from that. For example:

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

打来,说:

    sFile = Filename(f.SelectedItems(i), sPath)
    MsgBox sPath & "---" & sFile

完整

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

这篇关于ms access浏览文件并获取文件名和路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆