Results 1 to 6 of 6

Retrieve File Path/Directory

  1. #1
    SierraJuliet's Avatar
    SierraJuliet is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Dec 2017
    Location
    Earth
    Posts
    206

    Retrieve File Path/Directory

    Anyone have simple code for retrieving a file's path (directory + filename)?

    The concept is to select a button on the form that opens a dialog box for selecting a file. When the file is selected the entire directory and the filename is output to a text box on the form.

  2. #2
    GinaWhipp's Avatar
    GinaWhipp is online now Competent Performer
    Windows 7 64bit Access 2013 32bit
    Join Date
    Jul 2011
    Location
    Ohio, USA
    Posts
    313
    Hmm, I don't know about simple but hopefully...
    https://www.access-diva.com/d15.html
    Gina Whipp
    Microsoft MVP (Access 2010-2015)
    https://www.access-diva.com/tips.html

  3. #3
    accesstos's Avatar
    accesstos is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    160
    Quote Originally Posted by SierraJuliet View Post
    Anyone have simple code for retrieving a file's path (directory + filename)?
    Maybe the code below is what you need:
    Code:
    Function GetFileNameOnly(strPath As String) As String
        GetFileNameOnly = Mid$(strPath, InStrRev(strPath, "\") + 1)
    End Function
    
    Function GetDirectoryOnly(strPath) As String
        GetDirectoryOnly = Left$(strPath, InStrRev(strPath, "\"))
    End Function

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    6,021
    There must be hundreds of code examples for this. Suggest you find one that you like and copy it. Many use the msoFileDialogFilePicker or msoFileDialogFolderPicker if you want to search on that. I might be missing something with respect to the last post but can't see how that helps you to do what you want from a button click. IIRC, the function of the file picker is to return the complete path to the file while the folder picker only returns the folder path.
    - "doesn't work" is no help. Post err msgs and where.
    - Use code tags for code/sql. Implement changes in copies of your database.
    Sometimes I talk to myself - like when I need expert advice.

  5. #5
    SierraJuliet's Avatar
    SierraJuliet is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Dec 2017
    Location
    Earth
    Posts
    206
    This is the code I use already and it works great. This code is designed for a button to open a file picker dialog. When the button is clicked, a file dialog window appears for the user to navigate to the file and select it. Once the file is selected, the user clicks "Open" and the entire directory including the filename is added to a specified text box, which stores text as a hyperlink and allows the file to be opened simply by clicking on it. The directory and filename is saved in the database rather then the file itself. The file may be stored anywhere on a computer, network share drive, file server, or etcetera. The downside is if the file is moved the link will not work; therefore, some sort of link verification may be necessary to alert the user if the file is moved.

    Code:
    Private Sub btnFileBrowse_Click()
      With Application.FileDialog(1)
        .Title = "Choose file"
        If .Show Then
          Me.txtTextBoxName = .SelectedItems(1)
        End If
      End With
    End Sub

  6. #6
    accesstos's Avatar
    accesstos is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    160
    If the txtTextBoxName has the stored directory and filename, paste this function in a standard code module:
    Code:
    Function DirLen(strPath As String) As Integer
        DirLen = Len(Dir(strPath))
    End Function
    and set the expression DirLen([txtDir])=0 for the conditional formatting of txtTextBoxName. Choose a font and a fill color to indicate to the user that the file does not exists.

    I hope it helps!

    Cheers,
    John

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Create a Directory Path from calculated field value
    By soonerborn77 in forum Programming
    Replies: 1
    Last Post: 11-17-2018, 06:15 PM
  2. Replies: 6
    Last Post: 09-14-2017, 07:31 AM
  3. Merge directory path with field value
    By Eeeeeemil in forum Forms
    Replies: 3
    Last Post: 04-03-2017, 10:01 AM
  4. Replies: 1
    Last Post: 08-15-2015, 10:09 AM
  5. Replies: 2
    Last Post: 03-16-2014, 02:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Tech Forums: Microsoft Office Forums