ms-access - 在Access VBA中使用“浏览文件”对话框

标签 ms-access vba

我看到了How to show "Open File" Dialog in Access 2007 VBA?这个主题,我喜欢那里不使用引用的解决方案,但是,我不知道如何显示用户选择的文件路径。有人可以解释一下吗

非常感谢你

这是我正在谈论的那一部分

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

最佳答案

首先,您应该始终尽可能使用强类型变量。在这种情况下,您可以将Object替换为Office.FileDialog

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

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属性指定过滤器,这将限制用户在对话框中可以查看和选择的文件类型。例如,如果要将选择限制为仅 Access 数据库,则可以添加以下代码:
' Clear out the current filters
f.Filters.Clear

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

关于ms-access - 在Access VBA中使用“浏览文件”对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4813598/

相关文章:

php - 将 mysql 数据库导出到 Microsoft Access (.mdb)

ms-access - Access : Text is truncated in display of a textbox with Can Grow=Yes in report

excel - 四舍五入到最接近的 N 个数字

VBA,高级过滤器工作簿,填充到工作表中的公共(public)列中

VBA Variant - IsNull 和 IsEmpty 对于空数组均为 false

ms-access - 如何在 Word VBA 中使用使用 Access-VBA 定义的函数的 Access 查询?

java - Access/更新共享数据库

excel - 处理时间是指数非线性的? (VBA 集合)

c++ - 海量数据插入SQL Server?

excel - 做算术运算时如何获得相等的结果vba/excel [双变量精度]