将文件浏览器按钮添加到MS Access表单 [英] Adding a file browser button to a MS Access form

查看:103
本文介绍了将文件浏览器按钮添加到MS Access表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to add a "Browse" button to an MS Access 2007 form that will bring up a standard Windows file browser (as a modal window) and allow the user to select a directory. When the user OKs out of that browser, the path the the selected directory should be written to a text box in the Access form.

执行此操作的最佳方法是什么?有本地访问方法吗?

What's the best way to do this? Is there a native Access way?

推荐答案

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

Create a function which uses Application.FileDialog. The FileDialog is modal.

如果选择了此功能,则将返回用户的文件夹选择;如果单击了FileDialog上的取消",则将返回一个空字符串.

This function will return the user's folder selection if they made one, or an empty string if they clicked cancel on the 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

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

I think you can use that function in your command button's click event.

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

如果您担心微软有朝一日可能会从Office中删除FileDialog对象,则可以改用Windows API方法:

If you're concerned that Microsoft may remove the FileDialog object from Office someday, you can use the Windows API method instead: BrowseFolder Dialog.

这篇关于将文件浏览器按钮添加到MS Access表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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