How do I trim a path

G

Gary Hull

I am saving the path to some photos in a Access Database

I would like a field on a form to just show the file name less the path

Thanks for the help
 
T

TonyT

Hi Gary,

You can use the Right function in a loop to find the last / and then show
text to the right of that;

For i = 1 To Len(strContainingPath)
If Right(strContainingPath, i)= "/" Then
Me.txtFieldShowingPath = Mid(strContainingPath,
Len(strContainingPath) - i + 2, i - 1))
End If
Loop

hope that helps,

TonyT..
 
D

Douglas J. Steele

I don't think your code will work, Tony.

First of all, you're using the Right function, which will return an
ever-increasing substring, so it will never actually equal "\". And once
you've found the position of the slash, i is pointing to that slash, so you
want the Mid function to retrieve what's after that slash (i.e. i + 1) to
the end (which means you can skip supplying the last argument). You're also
not exiting the loop once you've found the first slash. I think you need:

For i = Len(strContainingPath) To 1 Step -1
If Mid(strContainingPath, i, 1)= "/" Then
Me.txtFieldShowingPath = _
Mid(strContainingPath, i + 1)
Exit For
End If
Loop

However, Access 2000 and newer have a InStrRev function that would probably
be more efficient:

Me.txtFieldShowPath = Mid(strContainingPath, InStrRev(strContainingPath,
"\") + 1)

And, as long as the file actually exists, in all versions of Access, you can
use the Dir function to return only the file name from a full path:

Me.txtFieldShowPath = Dir(strContainingPath)
 
T

TonyT

Knew I should have checked it before rushing out with the missus!!!!

oops, sorry Gary, thanks Douglas!

TonyT..
 
Top