Link to home
Start Free TrialLog in
Avatar of keschuster
keschuster

asked on

MS Access VBA InStrRev question

I'm getting incorrect results from InStrRev.  It should return 9 every time.  Any Ideas

?InStrRev("C:\test\te.st.txt", "\")
returns 8

?InStrRev("C:\te.st\te.st.txt", "\")
Returns 9

The only difference is a period added into the path name.
Avatar of thenelson
thenelson

InStrRev is correct. In the first case, "\" is the 8th character from the front. In the second case, it in the 9th character from the front. InStrRev searches from back to front but returns the character position count from the front. If you went the count from the back use:
Len (YourString) - InStrRev("YourString", "\")
Same here.
Adding lots of . into the string doesn't change the result. Looks like a bug at the moment.
Avatar of keschuster

ASKER

here's the function I'm using this in.  

Input
?returnfilepath("C:\test\te.st.txt")
 17
 8
C:\test\t

?returnfilepath("C:\te.st\te.st.txt")
 18
 9
C:\te.st\

Why am I getting the extra "t" in the first one
Public Function ReturnFilePath(Filename As Variant)
    'returns the path
    Dim ln As Integer
    If Filename <> "" Then
        ln = Len(Filename)
        Debug.Print ln
        Debug.Print InStrRev(Filename, "\")
        ReturnFilePath = Left(Filename, ln - InStrRev(Filename, "\"))
    End If
End Function

Open in new window

And it looks like I can't count- back to sleep Peter!
Try this function


Public Function getpath(FileName As String) As String
getpath = (Mid(FileName, 1, Len(FileName) - Len(Dir(FileName))))
End Function
Hello keschuster,

You are getting confused.  The len - position is a requirement of the right method.  For lewft you want the position of the "\" to the left therefore use:

Regards,

chris_bottomley
Public Function ReturnFilePath(Filename As Variant)
    'returns the path
    Dim ln As Integer
    If Filename <> "" Then
        ln = Len(Filename)
        Debug.Print ln
        Debug.Print InStrRev(Filename, "\")
        ReturnFilePath = Left(Filename, InStrRev(Filename, "\") - 1)
    End If
End Function

Open in new window

TheNelson
Not working it's returning

C:\test\te.st.txt
C:\te.st\te.st.txt

Need everything after \ stripped off
Chris
Test both inputs

C:\test\te.st.txt
C:\te.st\te.st.txt

two different results
ASKER CERTIFIED SOLUTION
Avatar of thenelson
thenelson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial