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

Returning only the file name

P: 15
I am trying to get only the filename in the [file] but it returns the path and filename. I am new at Access and need help. What should I do?

Private Sub File_Click()
Dim fileName As String
Dim result As Integer
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Choose file"
.Filters.Add "All", "*.pdf"
.FilterIndex = 2
.AllowMultiSelect = False
.InitialFileName = "c:\"
result = .Show
If (result <> 0) Then
fileName = Trim(.SelectedItems.Item(1))
Me![Name].SetFocus
Me![Name].Text = fileName
End If
End With
End Sub
May 19 '07 #1
Share this Question
Share on Google+
5 Replies


ADezii
Expert 5K+
P: 8,619
I am trying to get only the filename in the [file] but it returns the path and filename. I am new at Access and need help. What should I do?

Private Sub File_Click()
Dim fileName As String
Dim result As Integer
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Choose file"
.Filters.Add "All", "*.pdf"
.FilterIndex = 2
.AllowMultiSelect = False
.InitialFileName = "c:\"
result = .Show
If (result <> 0) Then
fileName = Trim(.SelectedItems.Item(1))
Me![Name].SetFocus
Me![Name].Text = fileName
End If
End With
End Sub
Insert this Line of Code after fileName = Trim(.SelectedItems.Item(1))
Expand|Select|Wrap|Line Numbers
  1. strFileNameOnly = Right$(FileName, Len(FileName) - InStrRev(FileName, "\"))
'Replace Me![Name].Text = fileName with
Expand|Select|Wrap|Line Numbers
  1. Me![Name].Text = strFileNameOnly
May 19 '07 #2

P: 15
Thank you for your response.
That worked, but when I put in Option Explicit at the top, the "strFileNameOnly =" got highlighted and the folowing error was shown:

Compile Error:
Variable not defined

Why?
May 19 '07 #3

ADezii
Expert 5K+
P: 8,619
Thank you for your response.
That worked, but when I put in Option Explicit at the top, the "strFileNameOnly =" got highlighted and the folowing error was shown:

Compile Error:
Variable not defined

Why?
That's because you never Declared it in the Procedure:
Expand|Select|Wrap|Line Numbers
  1. Dim strFileNameOnly As String
May 19 '07 #4

P: 15
That's because you never Declared it in the Procedure:
Expand|Select|Wrap|Line Numbers
  1. Dim strFileNameOnly As String

Thanks a lot for your help, everything is working as it should now : )
May 20 '07 #5

ADezii
Expert 5K+
P: 8,619
Thanks a lot for your help, everything is working as it should now : )
Glad it all worked out.
May 20 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.