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