2011-06-01 126 views
9

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

这样做的最好方法是什么?是否有本地访问方式?

回答

12

创建一个使用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 

如果你担心,微软可能会罢免FileDialog对象有一天,你可以改用Windows API的方法:BrowseFolder Dialog

+0

工作。谢谢! – 2011-06-01 17:21:54

+0

我总是建议使用Windows API,因为我不相信MS有一天不从Office中删除FileDialog对象,因为他们从Office 2007中删除了FileSearch对象。 – 2011-06-03 03:06:15