在MS Access表单中添加文件浏览器按钮

10
我想在MS Access 2007表单中添加一个“浏览”按钮,它会弹出一个标准的Windows文件浏览器(作为模态窗口),并允许用户选择一个目录。当用户从该浏览器中点击确定后,所选目录的路径应写入Access表单中的文本框。 如何做到最好?是否有原生的Access方式可供选择?
1个回答

17
创建一个使用 Application.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对话框


我总是建议使用Windows API,因为我不相信微软不会在某一天从Office中删除FileDialog对象,就像他们在Office 2007中删除了FileSearch对象一样。 - David-W-Fenton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接