Solved

Use a command button to browse to a directory and then have that directory path populate a text box.

Posted on 2015-01-15
4
143 Views
Last Modified: 2015-01-15
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

Open in new window

0
Comment
Question by:SteveL13
4 Comments
 
LVL 12

Assisted Solution

by:James Elliott
James Elliott earned 250 total points
ID: 40551203
Try swapping msoFileDialogFilePicker for msoFileDialogFolderPicker
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 40551205
HI,

you should use
 msofiledialogfolderpicker

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
            Me.txtPDFfilePath =  .SelectedItem
         Next
      Else
         MsgBox "You clicked Cancel in the file dialog box."
      End If
   End With
End Sub

Open in new window

Regards
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40551252
I have the following code in place but it is not doing what I want it to do.
Can you clearly state what it is, ...or is, not doing?

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:
'************** Code Start **************
'This code was originally written by Terry Kreft.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code courtesy of
'Terry Kreft

Private Type BROWSEINFO
  hOwner As Long
  pidlRoot As Long
  pszDisplayName As String
  lpszTitle As String
  ulFlags As Long
  lpfn As Long
  lParam As Long
  iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
            "SHGetPathFromIDListA" (ByVal pidl As Long, _
            ByVal pszPath As String) As Long
            
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
            "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
            As Long
            
Private Const BIF_RETURNONLYFSDIRS = &H1
Public Function BrowseFolder(szDialogTitle As String) As String
  Dim X As Long, bi As BROWSEINFO, dwIList As Long
  Dim szPath As String, wPos As Integer
  
    With bi
        .hOwner = hWndAccessApp
        .lpszTitle = szDialogTitle
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With
    
    dwIList = SHBrowseForFolder(bi)
    szPath = Space$(512)
    X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
    
    If X Then
        wPos = InStr(szPath, Chr(0))
        BrowseFolder = Left$(szPath, wPos - 1)
    Else
        BrowseFolder = vbNullString
    End If
End Function
'*********** Code End *****************

Open in new window


Then put code like this on your form (button)
Dim strFolderName As String

    strFolderName = BrowseFolder("Choose Folder For Import")

    If Len(strFolderName) > 0 Then
        ' Do something with the selected folder
        Me.txtPDFfilePath = strFolderName
    Else
        'No folder chosen, or user canceled
        MsgBox "You clicked Cancel in the file dialog box."
    End If

Open in new window


Hope this helps some...

JeffCoachman
0
 

Author Comment

by:SteveL13
ID: 40551264
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

Open in new window

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sending email from List Data 2 51
Direct Mail software 4 46
ms/access webbrowser address not valid  - 12 37
Conditional lock of one field in  datasheet 9 22
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now