Results 1 to 12 of 12
  1. #1
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Getting just the file name (Access 2003)

    I have an application with the following code that I either lifted from the Northwind sample db or some Lounger who very kindly sent it to me. It opens a dialog box and lets the operator select a file. It puts the entire filespec (i.e. Cocuments and Settingsetc.filename.xxx) in the string fileName. Is there similar code that only retrieves the file name and extension without the entire filespec?

    Sub getFilename()
    Dim fileName As String
    Dim result As Integer
    Dim dlg As New CommonDialog
    With dlg
    .fileName = "*.jpg"
    .InitDir = CurrentProject.path & "Pictures"
    .Filter = "Image Files (*.jpg)|*.bmp|All Files (*.*)|*.*"
    .Flags = cdlHideReadOnly Or cdlFileMustExist Or cdlPathMustExist
    If .OpenDialog() = True Then
    fileName = Trim(.fileName)
    Me![ImagePath].Visible = True
    Me![ImagePath].SetFocus
    Me![ImagePath].Text = fileName
    Me![Fname].SetFocus
    Me![ImagePath].Visible = False
    Else
    MsgBox "No file selected", vbInformation
    End If
    End With
    Set dlg = Nothing
    End Sub

  2. #2
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Getting just the file name (Access 2003)

    Just add an extra line to your current function

    filename =Right([filename],Len([filename])-InStrRev([filename],"/"))

    This returns what comes after the last / in the filename.

    Put this after the line filename = trim(filename)
    Regards
    John



  3. #3
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Getting just the file name (Access 2003)

    Don't you want to find a (back slash) rather than a / (forward slash)?

  4. #4
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Getting just the file name (Access 2003)

    Oops. Make that:


    filename =Right([filename],Len([filename])-InStrRev([filename],""))
    Regards
    John



  5. #5
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Getting just the file name (Access 2003)

    John, does InstrRev search for a string in reverse?

    Are you sure your expression is correct?
    Don't you want to find the number of characters from the right hand side of the string and use that for the length, eg InStrRev([filename],"")

  6. #6
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Getting just the file name (Access 2003)

    John's function works perfectly if you remove the square brackets as filename is a variable and not a field.
    Francois

  7. #7
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Getting just the file name (Access 2003)

    My mistake, I thought that InstrRev returned a number of characters from the right and not the left. <img src=/S/blush.gif border=0 alt=blush width=15 height=15>

  8. #8
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Getting just the file name (Access 2003)

    Thanks. Your suggestion works just fine once the slash was changed into a back slash.

  9. #9
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Getting just the file name (Access 2003)

    If it is an existing file and you have access to the directory, Dir(<full path and filename>) will return just the filename. It has the added benefit of verifying the existence of the file at the same time.
    Charlotte

  10. #10
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Getting just the file name (Access 2003)

    Thanks for the suggestion, Charlotte. What is the correct syntax? Something like this?

    Fname = Dir(C:.pathfilename.xxx)

  11. #11
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Getting just the file name (Access 2003)

    You have to surround the path and filename with quotes.
    Fname = Dir("C:.pathfilename.xxx")
    or in your code you can use the filename variable without quotes, enter these just after the line
    fileName = Trim(.fileName)
    Fname = Dir(filename)
    Francois

  12. #12
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Getting just the file name (Access 2003)

    I got it Francois -- Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •