Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a file browser button to a MS Access form

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?

like image 571
Isaac Moses Avatar asked Jun 01 '11 15:06

Isaac Moses


People also ask

How do I add a Button to a form in Access?

Add a command button to a form by using a wizardOn the Design tab, in the Controls group, click Button. In the design grid, click where you want the command button to be inserted. The Command Button Wizard starts. Follow the directions in the wizard.

What is Web browser control in MS Access?

Use the Web Browser Control to specify a URL and show web pages on a form. You can map Access fields to URL parameters and create dynamic web pages for each form record. you can also browse files and folders by specifying a file URL.

How do I open an Access form in a browser?

On the File tab, under Help, click Options. In the Access Options dialog box, click Current Database. Under Application Options, click Web Display Form, and then select the form that you want from the list. Note: You do not have to select your navigation form as the web display form.


1 Answers

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

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

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

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

like image 169
HansUp Avatar answered Nov 15 '22 21:11

HansUp