2

I have an Access database that stores file paths. Sometimes the documents get thrown into an "archive" sub folder. I've created a form that loads the record with the file name and file path. On the form I created a button that adds \archive\ into the existing file path. The \archive\ needs to be inserted after the last backslash found in the file path. Here's the function I've tried but I can't find the solution anywhere.

Public Function insertArch (ByVal strPath As String) insertArchive = InStrRev(strPath, "\") + " archive\" End Function

I'm trying to find the first backslash from the end of the string, and then want to insert archive\. What am I doing wrong?

0

1 Answer 1

4

InStrRev returns a character offset, not a string.

You can:

Public Function insertArchive(ByVal strPath As String) As String
    Dim pos As Long
    pos = InStrRev(strPath, "\")
    If (pos > 0) Then
        insertArchive = Left$(strPath, pos) & "archive\" & Mid$(strPath, pos + 1)
    Else
        insertArchive = strPath
    End If
End Function

For:

?insertArchive("C:\foo\bar\qux.dat")
C:\foo\bar\archive\qux.dat
Sign up to request clarification or add additional context in comments.

1 Comment

Works flawlessly. Appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.