在 Access VBA 中使用浏览文件对话框 [英] Using the browse for file dialog in Access VBA

查看:25
本文介绍了在 Access VBA 中使用浏览文件对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这个话题 如何显示打开文件"Access 2007 VBA 中的对话框? 我喜欢那里不使用引用的解决方案,但是,我不知道如何显示用户选择的文件路径.谁能解释一下

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

非常感谢

这就是我要说的部分

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

推荐答案

第一件事:你应该总是尽可能使用强类型变量.在这种情况下,您可以将 Object 替换为 Office.FileDialog.

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

要显示每个选定文件的路径,您需要遍历SelectedItems 集合.例如,您将添加以下代码:

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

请注意,如果您需要自定义,FileDialog 还具有您可以设置的其他属性.例如,.Title 属性允许您指定一个标题,该标题将在标题栏中显示为对话框的标题.您还可以使用 .Filter 属性指定过滤器,这将限制用户能够在对话框中查看和选择的文件类型.例如,如果您想将选项限制为仅访问数据库,您可以添加以下代码:

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", "*.*"

这篇关于在 Access VBA 中使用浏览文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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