Results 1 to 15 of 15

Thread: using relative file paths instead of absolute

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    20

    using relative file paths instead of absolute

    Hi,

    I had got the code below to insert a hyperlink to file location in a textbox (that i found after searching the different forums). The point here is that I need to store the relative file path instead of the absolute path, but whenever I use it the text box displays the absolute path (C:\Documents\...). Any help.

    Code:
    Private Sub cmd_browse_Click()
        Dim varItem As Variant
        Dim strPath As String
        Dim filePicker As FileDialog
    
    Set filePicker = Application.FileDialog(msoFileDialogFilePicker)
    
    With filePicker
    
    'setup File Dialog'
    .AllowMultiSelect = False
    .ButtonName = "Select"
    .InitialView = msoFileDialogViewList
    .Title = "Select File"
    .InitialFileName = "C:\"
    
    'add filter for all files'
    With .Filters
    .Clear
    .Add "All Files", "*.*"
    End With
    .FilterIndex = 1
    
    'display file dialog box'
    .Show
    
    End With
    
    If filePicker.SelectedItems.Count > 0 Then
    
    Dim selectedFile As String
    selectedFile = filePicker.SelectedItems(1)
    GetAbsolutePath = Replace(selectedFile, ".\", CurrentProject.Path & "\")
    Me.exp_inv = selectedFile
    
    End If
    End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: using relative file paths instead of absolute

    Thread moved from the 'Database Development' forum (which is for questions about tables/SQL/etc) to the 'Office Development/VBA' forum (which is where other Access based questions belong)

  3. #3
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: using relative file paths instead of absolute

    When I test this out, this line:

    Code:
    GetAbsolutePath = Replace(selectedFile, ".\", CurrentProject.Path & "\")
    doesn't do anything, because I have no ".\" to replace.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    20

    Re: using relative file paths instead of absolute

    That is exactly what I'm getting, nothing is been done when that code is used.

  5. #5
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: using relative file paths instead of absolute

    you could do something like this:

    Code:
    Dim splits() As String
    Dim fName As String
    
    splits = Split(selectedFile, "\")
    fName = splits(UBound(splits))
    
    getabsolutepath = CurrentProject.Path & "\" & fName

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    20

    Re: using relative file paths instead of absolute

    Thank you for that, so should I keep the following statement as it is?
    Code:
    Me.exp_inv = selectedFile

  7. #7
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: using relative file paths instead of absolute

    I don't know what "Me.exp_inv" is, or what (if anything) is done with its value.

    What is it?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    20

    Re: using relative file paths instead of absolute

    I missed that part, it is the text box that shows the hyperlink.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: using relative file paths instead of absolute

    so should I keep the following statement as it is?
    no that would still return the absolute path

    try like
    Code:
    Me.exp_inv = GetAbsolutePath
    while the suggestions posted above may be adequate for your needs, they are not a real solution to thread title
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: using relative file paths instead of absolute

    Agreed, Pete. We addressed it if the title were more like "How do you split a string and then concatenate part of that string with another string?"

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    20

    Re: using relative file paths instead of absolute

    Thank you all for the reply. What I'm looking for here is to store the link to files in a database using the relative file path instead of the absolute path. so I don't want to store it using something like C:\ but instead \Application Data\Adobe\Acrobat

  12. #12
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: using relative file paths instead of absolute

    So you want a reference to the "generic part of the file path," so that no matter whose computer the "app" is running from, it will know where the files are? Like in either of these two scenarios:

    C:\Users\user123\Documents\testFile.txt

    C:\Users\user456\Documents\testFile.txt

    You'd want to see "Documents\testFile.txt" for example?

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    20

    Re: using relative file paths instead of absolute

    Yes, that what I'm exactly looking for.

  14. #14
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: using relative file paths instead of absolute

    You could do something like this, using the Documents folder as an example. Note that it would not be the same for all versions of Windows.

    Code:
    If filePicker.SelectedItems.Count > 0 Then
    
    Dim selectedFile As String
    Dim pathMinusFile As String
    Dim pathUnique As String
    Dim locDoc As Integer
    Dim lenSelected As Integer
    
    selectedFile = filePicker.SelectedItems(1)
    lenSelected = Len(selectedFile)
    
    On Error Resume Next
    locDoc = InStr(LCase(selectedFile), "documents")
    If Err.Number <> 0 Then     '   "documents" not found most likely ***
        MsgBox Err.Description
        Exit Sub
    End If
    
    pathUnique = Right(selectedFile, lenSelected - locDoc + 1)
    Insert that into your current code and see what "pathUnique" is.

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: using relative file paths instead of absolute

    if you want to find current users folders or other system folders, you should be returning from the special folders collections

    to do this you can use API, wscript shell object or environ
    whichever best suits your needs
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width