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.t xt", "\")
returns 8
?InStrRev("C:\te.st\te.st. txt", "\")
Returns 9
The only difference is a period added into the path name.
?InStrRev("C:\test\te.st.t
returns 8
?InStrRev("C:\te.st\te.st.
Returns 9
The only difference is a period added into the path name.
Same here.
Adding lots of . into the string doesn't change the result. Looks like a bug at the moment.
Adding lots of . into the string doesn't change the result. Looks like a bug at the moment.
ASKER
here's the function I'm using this in.
Input
?returnfilepath("C:\test\t e.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
Input
?returnfilepath("C:\test\t
17
8
C:\test\t
?returnfilepath("C:\te.st\
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
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
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
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
ASKER
TheNelson
Not working it's returning
C:\test\te.st.txt
C:\te.st\te.st.txt
Need everything after \ stripped off
Not working it's returning
C:\test\te.st.txt
C:\te.st\te.st.txt
Need everything after \ stripped off
ASKER
Chris
Test both inputs
C:\test\te.st.txt
C:\te.st\te.st.txt
two different results
Test both inputs
C:\test\te.st.txt
C:\te.st\te.st.txt
two different results
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Len (YourString) - InStrRev("YourString", "\")