VBA to open file dialog and store path in text box (1 Viewer)

tmyers

Member
Local time
Today, 04:22
Joined
Sep 8, 2020
Messages
370
I have been having some issues with this one.

I made a text box and bound it to my field [FilePath]. I am trying to work out a button that when clicked, will open the file dialog window and let my users select a file and input the files path into the text box. I will then make an on double click event to open said file path.

I tried https://answers.microsoft.com/en-us...g-access/374b9f15-77c3-4348-bf75-676658c9bb6b, but the answer there cause a slew of errors the moment the code was put in, completely breaking the form in which the button resided (really weird).

So, what kind of code would I need to open a window, select a file, and store its path in my text box? I prefer to store the folder directory if possible. I know file dialog doesnt work for folders.

Could someone offer insight on this?
 

tmyers

Member
Local time
Today, 04:22
Joined
Sep 8, 2020
Messages
370
I stumbled across:
Code:
Private Sub DrawingLink_Click()

  Dim f As Object
  Dim strFile As String
  Dim strFolder As String
  Dim varItem As Variant

  Set f = Application.FileDialog(3)
  f.AllowMultiSelect = False
  If f.Show Then
    For Each varItem In f.SelectedItems
        strFile = Dir(varItem)
        strFolder = Left(varItem, Len(varItem) - Len(strFile))
        MsgBox "Folder: " & strFolder & vbCrLf & _
            "File: " & strFile
        Me.Link = strFolder & "\" & strFile
    Next
  End If
  Set f = Nothing
End Sub

This worked for one specific file. How could I modify that to add in the msoFileFolderPicker like you suggested?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:22
Joined
Oct 29, 2018
Messages
13,239
I stumbled across:
Code:
Private Sub DrawingLink_Click()

  Dim f As Object
  Dim strFile As String
  Dim strFolder As String
  Dim varItem As Variant

  Set f = Application.FileDialog(3)
  f.AllowMultiSelect = False
  If f.Show Then
    For Each varItem In f.SelectedItems
        strFile = Dir(varItem)
        strFolder = Left(varItem, Len(varItem) - Len(strFile))
        MsgBox "Folder: " & strFolder & vbCrLf & _
            "File: " & strFile
        Me.Link = strFolder & "\" & strFile
    Next
  End If
  Set f = Nothing
End Sub

This worked for one specific file. How could I modify that to add in the msoFileFolderPicker like you suggested?
Change one line to this and try it.
Code:
Set f = Application.FileDialog(4)
 

tmyers

Member
Local time
Today, 04:22
Joined
Sep 8, 2020
Messages
370
That did the trick!
As always, thanks for your help!
 

vhung

Member
Local time
Today, 01:22
Joined
Jul 8, 2020
Messages
159
Thanks for this code; I make use of it... for file path name

Dim f As Object
Dim strFile As String
Dim strFolder As String
Dim varItem As Variant

Set f = Application.FileDialog(1)
f.AllowMultiSelect = False

If f.show Then
For Each varItem In f.SelectedItems
strFile = Dir(varItem)
strFolder = Left(varItem, Len(varItem) - Len(strFile))
Me.[LinkAccdb] = strFolder & "" & strFile
Next
End If
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:22
Joined
Oct 29, 2018
Messages
13,239
Thanks for this code; I make use of it... for file path name

Dim f As Object
Dim strFile As String
Dim strFolder As String
Dim varItem As Variant

Set f = Application.FileDialog(1)
f.AllowMultiSelect = False

If f.show Then
For Each varItem In f.SelectedItems
strFile = Dir(varItem)
strFolder = Left(varItem, Len(varItem) - Len(strFile))
Me.[LinkAccdb] = strFolder & "" & strFile
Next
End If
Hi. Glad to hear you found it useful. Cheers!
 

vhung

Member
Local time
Today, 01:22
Joined
Jul 8, 2020
Messages
159

Attachments

  • Tableviewer.png
    Tableviewer.png
    218.6 KB · Views: 9

Users who are viewing this thread

Top Bottom