Trimming file path names

  • Thread starter Thread starter Jimmy G
  • Start date Start date
J

Jimmy G

I recently got this function from this Newsgroup: Public
Function GetFileNameFromFullPath(fullPath As String) As
String
GetFileNameFromFullPath = Right(fullPath, Len(fullPath) -
InStrRev(fullPath,
"\", , vbTextCompare))
End Function

I put the code into the form How do I get the resulting
fileName to show in my textfield?
 
I copied your code using PhotoName in place
of 'YourTextBox', nothing happens. There is something I'm
not getting.
 
What's fullPath?

I thought you want the name of the database which doesn't sound correct from
your post.

--
HTH
Van T. Dinh
MVP (Access)
 
The name of fullPath is the name of the field that holds
the full path to the photo file. I am look for the file
name without the full path(ie.
D:\MyDocuments\Pictures\Photo1.jpj). All I want to see
is 'Photo1' or 'Photo1.jpg.'
 
Sorry. I answered something else previously.

Is "fullPath" a VBA Variable or a Field name in your Table?
 
It is a Field Name.
-----Original Message-----
Sorry. I answered something else previously.

Is "fullPath" a VBA Variable or a Field name in your Table?

--
HTH
Van T. Dinh
MVP (Access)





.
 
If the Field "fullPath" is included in the Form's RecordSource, you can set
the Control of the ComboBox to:

= GetFileNameFromFullPath([fullPath])

Note: the function GetFileNameFromFullPath(fullPath As String) should be in
a *Standard Module* which means that you can use it everywhere in your
database, NOT the Module attached to the Form.
 
I put the function into a *Standard Module* and when I run
form I get the error message " Looking for a variable, not
a Module". This is the code in the module:

Function GetFileNameFromFullPath(PhotoLocation As String)
As String
GetFileNameFromFullPath = Right(PhotoLocation, Len
(PhotoLocation) - InStrRev(PhotoLocation, "\",
vbTextCompare))
End Function

I believe something is wrong with this syntax. What? I'm
not sure.

Also, this is the code in the OnCurrent property of the
form:

Me.PhotoName = GetFileNameFromFullPath(PhotoLocation)
-----Original Message-----
If the Field "fullPath" is included in the Form's RecordSource, you can set
the Control of the ComboBox to:

= GetFileNameFromFullPath([fullPath])

Note: the function GetFileNameFromFullPath(fullPath As String) should be in
a *Standard Module* which means that you can use it everywhere in your
database, NOT the Module attached to the Form.

--
HTH
Van T. Dinh
MVP (Access)




It is a Field Name.


.
 
You didn't name the module "GetFileNameFromFullPath", did you?

The modules and functions share the same name space and VBA will give you
errors if you use the name for Module and Function. I nomally prefix the
Module name with "mod" or "vba" and Function name with "fn" to make sure I
don't have duplicated names.

Also, I noticed that you need an extra comma in InStrRev like:

InStrRev(PhotoLocation, "\", ,vbTextCompare)

Personally, I simply use:

InStrRev(PhotoLocation, "\")

Check Access VB Help on the InStrRev() function. Also, check on how to use
the Immediate / Debug window where you can test your functions quickly.
Most Access VBA / programming will have info. on the Debug / Immediate
window.
 
I would like for you to send me a simple function to set into my module to test my Access program.. So far, nothing I have put into a function has worked. I've built 7 Tables, including relationships between them, with cascading updates and deletes, 6 Queries, 12 Forms, 3 Sub-Forms and 6 Reports. They all work fine. The only thing I want to do is get a text box to show a short file name with data derived from a text box containing the complete to the file name. I'm missing something. This has been a frustrating experience for me
 
Your function *works fine* with the extra comma I mentioned.

Try typing this in the Debug/Immediate window:

?GetFileNameFromFullPath("C:\ABC\DEF.ext")

It should returns

DEF.ext

as you want.

Is "PhotoLocation" the name of the Field in the RecordSource of the Form or
the name of the TextBox Control on the Form? From your posts, I not sure
which one "PhotoLocation" refers to.

What do get in the TextBox PhotoName?

--
HTH
Van T. Dinh
MVP (Access)


Jimmy G said:
I would like for you to send me a simple function to set into my module to
test my Access program.. So far, nothing I have put into a function has
worked. I've built 7 Tables, including relationships between them, with
cascading updates and deletes, 6 Queries, 12 Forms, 3 Sub-Forms and 6
Reports. They all work fine. The only thing I want to do is get a text box
to show a short file name with data derived from a text box containing the
complete to the file name. I'm missing something. This has been a
frustrating experience for me.
 
Check the spelling of the function name in the Form_Current Event.

Also, when you get the error, press Ctrl + Break to get to the code window
and see which line of code is highlighted, usually the line that gives you
the error (unless you have error-trapping code).

1. Have you tried the Debug Test as I mentioned?

2. Any, in the Debug window, try:

?Date()

and see if you get any error. It is possible that you have Reference
Errors. See Douglas J. Steele's article:

http://members.rogers.com/douglas.j.steele/AccessReferenceErrors.html

3. Which version of Access are you using?

4. The function is in a Standard Module? You can see the Standard Modules
as well as Class Modules (except Code-Behind-Form Class Modules) in the
Modules tab of the database window. The icon for the Standard Modules looks
like fishbone with attached lollies. The icon for the Class Modules has a
rectangle.
 
This is the code in the Module tab of the database window:

Function GetFileNameFromFullPath(PhotoLocation As String)
As String
GetFileNameFromFullPath = Right(PhotoLocation, Len
(PhotoLocation) - InStrRev(PhotoLocation, "\", ,
vbTextCompare))
End Function

The statement :Getfile......=.......vbTextCompare" is on
one line.

In the OnCurrent property of the form is the line:
Me.PhotoName = GetFileNameFromFullPath

When I open the form, the code window of Visual Basic
comes up and the error message pops up and
displays: "Argument not optional", and
highlights "GetFileNameFromFullPath"

1. When I type in the immediate window: "?
GetFileNameFromFullPath", I get the error: "Compile error:
Argument not optional", after I press <enter>.

2. When I type: ?Date(), I get today's date(5/5/2004).

3. Access 2003.

4. This function is in the Module tab of the Database
window. There are no other modules visible.

All quotes of the code have been pasted so as to avoid
typo's.
 
I have found a solution:

1. I declared a public variable(ShortFileName) in the form
code.

2. Next I set ShortFileName = Right(PhotoLocation, Len
(PhotoLocation) - InStrRev(PhotoLocation, "\", ,
vbTextCompare))

3. Then I set PhotoName = ShortFileName.

It works. Is there any danger in using the Variable as
opposed to the Function?
 
I have found a solution:

1. I declared a public variable(ShortFileName) in the form
code.

2. Next I set ShortFileName = Right(PhotoLocation, Len
(PhotoLocation) - InStrRev(PhotoLocation, "\", ,
vbTextCompare))

3. Then I set PhotoName = ShortFileName.

It works. Is there any danger in using the Variable as
opposed to the Function?
 
1. If you look at the declaration of the Function:

Function GetFileNameFromFullPath(PhotoLocation As String) As String

The part inside the parentheses is the argument that the function / VBA
*expects* you to provide. If you look back to my earlier post, I asked you
to try:

?GetFileNameFromFullPath("C:\ABC\DEF.ext")

and "C:\ABC\DEF.ext" is the value that the function expects!

I didn't ask you to try:

?GetFileNameFromFullPath

since the function expects you to provide a value for the argument!

2. You filed names / code kept changing which make it harder to identify
the problem. Previously, you had:

Me.PhotoName = GetFileNameFromFullPath(PhotoLocation)

which is correct provided that PhotoLocation is a Field name in the Form's
RecordSource or a ControlName, i.e. there is a value that the function
expects.

On your last post, you had:

Me.PhotoName = GetFileNameFromFullPath

which did NOT supply a value for the required argument. Hence the error
occurred.

3. It should be fine with the variable ShortFileName but preferably not
public. In fact, you can simply use:

Me.PhotoName = Right(PhotoLocation, Len(PhotoLocation)
- InStrRev(PhotoLocation, "\", , vbTextCompare))

(all on 1 line). My guess is that "PhotName" is the name of a TextBox
Control on the Form.
 
Back
Top