Retrieve just the file name, not the full path

J

Jason Lim

Dear all,

I am trying to call the Windows dialogue box, select a file, and save
the file name, all by a click of a button. I refer to:
http://www.mvps.org/access/api/api0001.htm.

When the button is click, the event procedure is:

Private Sub cmdSelect_Click()

Dim strStartDir As String

Dim strFilter As String
Dim lngFlags As Long

' Lets start the file browse from our current directory

strStartDir = CurrentDb.Name
strStartDir = Left(strStartDir, Len(strStartDir) - Len(Dir
(strStartDir)))


strFilter = ahtAddFilterItem(strFilter, _
"All Files (*.*)", "*.*")
Me.txtFileName = ahtCommonFileOpenSave(InitialDir:=strStartDir, _
Filter:=strFilter, FilterIndex:=3,
Flags:=lngFlags, _
DialogTitle:="Select File")

-------------------------------------------------------------------------------------------
But, I just want to retrive the file name, not the full path.
Is there any way of doing it?
Please help, as I really know nothing about VBA.
Very sorry, and thanks a lot in advance.

=)
 
A

Allen Browne

Use InstrRev() to find the final slash.

Or Dir() might parse the file name from the full path.
 
D

Daniel Pineault

You can use the following.

'---------------------------------------------------------------------------------------
' Procedure : GetFileName
' Author : CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Return the filename from a path\filename input
' Refs : http://www.applecore99.com/gen/gen029.asp
' Copyright : The following may be altered and reused as you wish so long as
the
' copyright notice is left unchanged (including Author, Website
and
' Copyright). It may not be sold/resold or reposted on other
sites (links
' back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' strFileWPath - string of a path and filename (ie: "c:\temp\test.xls")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
'
**************************************************************************************
' 1 2008-Feb-06 Initial Releas
'---------------------------------------------------------------------------------------
Function GetFileName(strFileWPath As String)
On Error GoTo GetFileName_Error

GetFileName = Right(strFileWPath, Len(strFileWPath) -
InStrRev(strFileWPath, "\"))

If Err.Number = 0 Then Exit Function

GetFileName_Error:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: ModExtFiles / GetFileName" & vbCrLf
& _
"Error Description: " & Err.Description, vbCritical, "An Error has
Occured!"
Exit Function
End Function

Simply pass the function the string returned by your
ahtCommonFileOpenSave... and pass its' output to your form control.
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
 
J

Jason Lim

Hye, thanks a lot for the replies.

I am very sorry, but i am really illiterate at VBA. Hmm what do you
mean by passing the function the string returned by
ahtCommonFileOpenSave?

Where do i actually use the above codes? In my ModOpenFileAPI module,
or right under the event procedure of the clicked button?

" Function GetFileName(strFileWPath As String) "

do i need to change strFileWPath to ahtCommonFileOpenSave?

and lastly, how to pass the output to my textbox? Is it
Me.txtFileName = GetFileName ?

Very sorry...
Thanks a lot in advance...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top