By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,491 Members | 879 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,491 IT Pros & Developers. It's quick & easy.

Capturing File Name As Opposed To The Full File Path

P: n/a
I am using code kindly provided by the Access Web to capture a file
path and insert it into a form. The relevant part of the code follows:
Function GetOpenFile(Optional varDirectory As Variant, _
Optional varTitleForDialog As Variant) As Variant
' Here's an example that gets an Access database name.
Dim strFilter As String
Dim lngFlags As Long
Dim varFileName As Variant
' Specify that the chosen file must already exist,
' don't change directories when you're done
' Also, don't bother displaying
' the read-only box. It'll only confuse people.
lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
If IsMissing(varDirectory) Then
varDirectory = ""
End If
If IsMissing(varTitleForDialog) Then
varTitleForDialog = ""
End If

' Define the filter string and allocate space in the "c"
' string Duplicate this line with changes as necessary for
' more file templates.
strFilter = ahtAddFilterItem(strFilter, _
"All Files (*.*)", "*.*")
' Now actually call to get the file name.
varFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
InitialDir:=varDirectory, _
Filter:=strFilter, _
Flags:=lngFlags, _
DialogTitle:=varTitleForDialog)
If Not IsNull(varFileName) Then
varFileName = TrimNull(varFileName)
End If
GetOpenFile = varFileName

Forms!Form1!FileTxt = GetOpenFile

End Function
The textbox Form1!FileTxt then shows the full path of the file. In
addition to this, I want to capture the file name only eg. If the path
is "C:\Docs\MyDoc.doc", I want another text box to show "Mydoc".

Can this be easily done?

Any help would be greatly appreciated.
Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In Access 2000 and later, use InstrRev() to find the last backslash
character in the file name. Then use Mid() to get the remainder as the file
name.

In earlier versions, use the trick that Dir() returns just the file name
from a full path.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Wayne Aprato" <cq*******@volcanomail.com> wrote in message
news:4d*************************@posting.google.co m...
I am using code kindly provided by the Access Web to capture a file
path and insert it into a form. The relevant part of the code follows:
Function GetOpenFile(Optional varDirectory As Variant, _
Optional varTitleForDialog As Variant) As Variant
' Here's an example that gets an Access database name.
Dim strFilter As String
Dim lngFlags As Long
Dim varFileName As Variant
' Specify that the chosen file must already exist,
' don't change directories when you're done
' Also, don't bother displaying
' the read-only box. It'll only confuse people.
lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
If IsMissing(varDirectory) Then
varDirectory = ""
End If
If IsMissing(varTitleForDialog) Then
varTitleForDialog = ""
End If

' Define the filter string and allocate space in the "c"
' string Duplicate this line with changes as necessary for
' more file templates.
strFilter = ahtAddFilterItem(strFilter, _
"All Files (*.*)", "*.*")
' Now actually call to get the file name.
varFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
InitialDir:=varDirectory, _
Filter:=strFilter, _
Flags:=lngFlags, _
DialogTitle:=varTitleForDialog)
If Not IsNull(varFileName) Then
varFileName = TrimNull(varFileName)
End If
GetOpenFile = varFileName

Forms!Form1!FileTxt = GetOpenFile

End Function
The textbox Form1!FileTxt then shows the full path of the file. In
addition to this, I want to capture the file name only eg. If the path
is "C:\Docs\MyDoc.doc", I want another text box to show "Mydoc".

Can this be easily done?

Any help would be greatly appreciated.

Nov 12 '05 #2

P: n/a
Thanks Allen. This works well.
Nov 12 '05 #3

P: n/a
If the file exist then Dir will return just the filename.

i.e.
?Dir("C:\Docs\MyDoc.doc")
MyDoc.doc
--
Terry Kreft
MVP Microsoft Access
"Wayne Aprato" <cq*******@volcanomail.com> wrote in message
news:4d*************************@posting.google.co m...
I am using code kindly provided by the Access Web to capture a file
path and insert it into a form. The relevant part of the code follows:
Function GetOpenFile(Optional varDirectory As Variant, _
Optional varTitleForDialog As Variant) As Variant
' Here's an example that gets an Access database name.
Dim strFilter As String
Dim lngFlags As Long
Dim varFileName As Variant
' Specify that the chosen file must already exist,
' don't change directories when you're done
' Also, don't bother displaying
' the read-only box. It'll only confuse people.
lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
If IsMissing(varDirectory) Then
varDirectory = ""
End If
If IsMissing(varTitleForDialog) Then
varTitleForDialog = ""
End If

' Define the filter string and allocate space in the "c"
' string Duplicate this line with changes as necessary for
' more file templates.
strFilter = ahtAddFilterItem(strFilter, _
"All Files (*.*)", "*.*")
' Now actually call to get the file name.
varFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
InitialDir:=varDirectory, _
Filter:=strFilter, _
Flags:=lngFlags, _
DialogTitle:=varTitleForDialog)
If Not IsNull(varFileName) Then
varFileName = TrimNull(varFileName)
End If
GetOpenFile = varFileName

Forms!Form1!FileTxt = GetOpenFile

End Function
The textbox Form1!FileTxt then shows the full path of the file. In
addition to this, I want to capture the file name only eg. If the path
is "C:\Docs\MyDoc.doc", I want another text box to show "Mydoc".

Can this be easily done?

Any help would be greatly appreciated.

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.