问题描述
我正在使用 ms access,我想添加一个按钮来浏览文件、获取文件名及其路径.然后我想将文件路径和文件名存储在 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 浏览文件并获取文件名和路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!