SteveL13
asked on
Use a command button to browse to a directory and then have that directory path populate a text box.
I have a command button on a form. When the user clicks the button I want an explorer window to appear allowing the user to browse to a directory. Then when they have gotten to the directory they want, have the path to that directory, not to a specific file in the directory, appear in a text box on the form. I have the following code in place but it is not doing what I want it to do.
Private Sub cmdClickToLocate_Click()
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = False
.Title = "Select the location of this client's pdf files."
.Filters.Clear
.Filters.Add "", "*.*"
If .Show = True Then
' Loop through each file that is selected and then add it to the list box.
For Each varFile In .SelectedItems
Me.txtPDFfilePath = varFile
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had to alter the proposed code a little to make it work...
Private Sub cmdClickToLocate_Click()
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select the location of this client's pdf files."
If .Show = True Then
For Each varFile In .SelectedItems
Me.txtPDFfilePath = varFile
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
I use a different, somewhat longer way to do this...
For my purposes, If I need to select a "folder" only, (and not a file,) , ...Then I use a dialog box that just allows me to select a Folder specifically.
The code is from here:
http://www.ammara.com/access_image_faq/code/browse_for_folder_code.txt
This goes in a public module:
Open in new window
Then put code like this on your form (button)
Open in new window
Hope this helps some...
JeffCoachman