【问题标题】:Adding a file browser button to a MS Access form将文件浏览器按钮添加到 MS Access 表单
【发布时间】:2011-09-06 10:14:13
【问题描述】:

我想在 MS Access 2007 表单中添加一个“浏览”按钮,该按钮将打开一个标准的 Windows 文件浏览器(作为模式窗口)并允许用户选择一个目录。当用户退出该浏览器时,应将所选目录的路径写入 Access 表单中的文本框中。

最好的方法是什么?有原生的Access方式吗?

【问题讨论】:

    标签: forms ms-access file-browser


    【解决方案1】:

    创建一个使用Application.FileDialog 的函数。 FileDialog 是模态的。

    如果用户选择了文件夹,则此函数将返回用户的文件夹选择,如果用户在FileDialog 上单击取消,则返回一个空字符串。

    Public Function FolderSelection() As String
        Dim objFD As Object
        Dim strOut As String
    
        strOut = vbNullString
        'msoFileDialogFolderPicker = 4
        Set objFD = Application.FileDialog(4)
        If objFD.Show = -1 Then
            strOut = objFD.SelectedItems(1)
        End If
        Set objFD = Nothing
        FolderSelection = strOut
    End Function
    

    我认为您可以在命令按钮的点击事件中使用该功能。

    Dim strChoice As String
    strChoice = FolderSelection
    If Len(strChoice) > 0 Then
        Me.TextBoxName = strChoice
    Else
        ' what should happen if user cancelled selection?
    End If
    

    如果您担心 Microsoft 可能有一天会从 Office 中删除 FileDialog 对象,您可以改用 Windows API 方法:BrowseFolder Dialog

    【讨论】:

    • 我总是建议改用 Windows API,因为我不相信 MS 有一天不会从 Office 中删除 FileDialog 对象,因为他们从 Office 2007 中删除了 FileSearch 对象。
    猜你喜欢
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2016-01-01
    • 2014-11-11
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    相关资源
    最近更新 更多