在Access VBA使用浏览文件对话框对话框、文件、Access、VBA

2023-09-08 10:56:37 作者:妆裙.

我看到这个题目How显示打开文件对话框在Access 2007 VBA?和我喜欢的解决方案,还有那不是使用引用,但是,我不知道如何来显示用户所选的文件路径。可有人请解释

非常感谢你。

这是一件我说的是

 昏暗˚F作为对象
集合F = Application.FileDialog(3)
f.AllowMultiSelect = TRUE
f.Show
MSGBOX文件choosen =&放大器; f.SelectedItems.Count
 

解决方案

首先:你应该总是preFER尽可能使用强类型的变量。在这种情况下,您可以替换对象 Office.FileDialog

access2007 窗体和VBA问题

要显示所选择的每个文件的路径,需要通过 SelectedItems 收集循环。例如,您可以添加以下code:

 昏暗˚F作为Office.FileDialog
集合F = Application.FileDialog(3)
f.AllowMultiSelect = TRUE

'显示的对话框。如果该方法返回true,用户选取至少一个文件。
如果该方法返回False,用户点击取消。
如果f.Show然后
    MSGBOX f.SelectedItems.Count和放大器; 文件(S)被选择。

    显示所选择的完整路径每个文件
    昏暗我作为整数
    对于i = 1到f.SelectedItems.Count
        MSGBOX f.SelectedItems(I)
    接下来我
结束如果
 

请注意,的FileDialog 还有其他的属性,你可以设置,如果你需要定制。例如, .title伪属性允许您指定将出现在标题栏的对话框的标题标题。您还可以指定使用 .Filter 属性,这将限制文件的用户将能够看到和选择对话框中的类型的过滤器。例如,如果你想的选择限制为仅Access数据库,您可以添加以下code:

 清除出当前过滤器
f.Filters.Clear

添加了一些自定义过滤器
f.Filters.AddAccess数据库,*。MDB
f.Filters.Add所有文件,*。*
 

i saw this topic How to show "Open File" Dialog in Access 2007 VBA? and i like the solution there that's not using references, however, i can't figure out how to display the file path that the user selected. can someone please explain

thank you very much

this is the piece i'm talking about

Dim f As Object   
Set f = Application.FileDialog(3)   
f.AllowMultiSelect = True   
f.Show    
MsgBox "file choosen = " & f.SelectedItems.Count

解决方案

First things first: you should always prefer to use strongly-typed variables whenever possible. In this case, you can replace Object with Office.FileDialog.

To display the paths of each file that was selected, you need to loop through the SelectedItems collection. For example, you would add the following code:

Dim f As Office.FileDialog
Set f = Application.FileDialog(3)   
f.AllowMultiSelect = True

' Show the dialog. If the method returns True, the user picked at least one file.
' If the method returns False, the user clicked Cancel.
If f.Show Then
    MsgBox f.SelectedItems.Count & " file(s) were chosen."

    ' Display the full path to each file that was selected
    Dim i As Integer
    For i = 1 To f.SelectedItems.Count
        MsgBox f.SelectedItems(i)
    Next i
End If

Note that the FileDialog also has other properties that you can set, if you require customization. For example, the .Title property allows you to specify a title that will appear as the dialog's caption in the title bar. You can also specify a filter using the .Filter property, which will limit the type of files that the user will be able to see and choose from in the dialog. For example, if you wanted to limit the choices to only Access Databases, you could add the following code:

' Clear out the current filters
f.Filters.Clear

' Add a few custom filters
f.Filters.Add "Access Databases", "*.mdb"
f.Filters.Add "All Files", "*.*"