putting the path from FileDialog into a text box

miconian

Well-known Member
Joined
Aug 18, 2004
Messages
769
So, what I want to do is, allow the user to browse the hard drive, pick a file, and then have the path to that file appear in a text box.

I found a popular solution that does something similar: it allows the user to choose multiple files, and it puts the paths for those files in a listbox. But I don't want to allow multiple choices. So I have attempted to modify the code to meet my purposes.

The original code that I'm referencing appears here:
http://msdn.microsoft.com/en-us/library/bb243865(v=office.12).aspx

Here is my modification. I am using a button called cmdText and a text box called txtPath.

Code:
Private Sub cmdText_Click()

   Dim fDialog As Office.FileDialog
   Dim varFile As Variant

   ' Clear listbox contents. '
   Me.txtPath.Value = ""

   ' Set up the File Dialog. '
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

   With fDialog

      ' Allow user to make multiple selections in dialog box '
      .AllowMultiSelect = False

      ' Set the title of the dialog box. '
      .Title = "Please select one file"

      ' Clear out the current filters, and add our own.'
      .Filters.Clear
      .Filters.Add "Access Databases", "*.MDB"
      .Filters.Add "Access Projects", "*.ADP"
      .Filters.Add "All Files", "*.*"

      ' Show the dialog box. If the .Show method returns True, the '
      ' user picked at least one file. If the .Show method returns '
      ' False, the user clicked Cancel. '
      If .Show = True Then

     'add selected path to text box
     Me.txtPath.Value = .SelectedItems

      Else
         MsgBox "You clicked Cancel in the file dialog box."
      End If
   End With
End Sub

Currently I'm getting a runtime error on this line:
Code:
Me.txtPath.Value = .SelectedItems

On that line, I am attempting to put the path into the text box. I'm obviously a bit clumsy with VBA in general, so I'm sure I'm missing some obvious step. Thanks!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
(Edit: Your message came up while I was searching for something else, I didnt notice the fact it was Access until I looked into the link you'd posted too. The code below is for Excel, I'm afraid I have no idea how much, if any, of it will work in access)

Hi,

I think this should do what you need;

Code:
Private Sub CmdText_Click()

    Dim SelFileName As String
    
    SelFileName = Application.GetOpenFilename(Title:="Please select a file")
    
    If SelFileName <> “False” Then
        txtPath.Value = Left$(SelFileName, InStrRev(SelFileName, "\"))
    End If
       
End Sub

Best regards
Richard
 
Last edited:
Upvote 0
Apparently, there is no GetOpenFilename method in the Access Application object.

I think I'm pretty close using FileDialog above. I just don't know how to assign the path the user chooses to a variable and therefore to the text box.
 
Upvote 0
Hi,

I´m not sure, but you can try this:

Code:
Me.txtPath.Value = .SelectedItems[COLOR=#0000FF](1)[/COLOR]
 
Upvote 0
Hey, thanks. That actually does seem to work.

I had a similar idea earlier, and tried to use .SelectedItems(0), but that didn't work. Why is the array's only item assigned to (1) and not (0)?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top