[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MS Access VBA InStrRev question

Posted on 2009-12-16
9
Medium Priority
?
911 Views
Last Modified: 2013-11-27
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.
0
Comment
Question by:keschuster
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 39

Expert Comment

by:thenelson
ID: 26063749
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", "\")
0
 
LVL 77

Expert Comment

by:peter57r
ID: 26063787
Same here.
Adding lots of . into the string doesn't change the result. Looks like a bug at the moment.
0
 

Author Comment

by:keschuster
ID: 26063796
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

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 77

Expert Comment

by:peter57r
ID: 26063798
And it looks like I can't count- back to sleep Peter!
0
 
LVL 39

Expert Comment

by:thenelson
ID: 26063891
Try this function


Public Function getpath(FileName As String) As String
getpath = (Mid(FileName, 1, Len(FileName) - Len(Dir(FileName))))
End Function
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 26063898
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

0
 

Author Comment

by:keschuster
ID: 26063950
TheNelson
Not working it's returning

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

Need everything after \ stripped off
0
 

Author Comment

by:keschuster
ID: 26063972
Chris
Test both inputs

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

two different results
0
 
LVL 39

Accepted Solution

by:
thenelson earned 2000 total points
ID: 26064034
Dir will only work if there in a file existing in the folder.

Try (Worked fine for your two samples) :

Public Function ReturnFilePath(Filename As Variant)
    'returns the path
    If Filename <> "" Then
        ReturnFilePath = Left(Filename, InStrRev(Filename, "\"))
    End If
End Function
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question