Capturing File Name As Opposed To The Full File Path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wayne Aprato

    Capturing File Name As Opposed To The Full File Path

    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(Opt ional varDirectory As Variant, _
    Optional varTitleForDial og 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_FILEMUST EXIST Or _
    ahtOFN_HIDEREAD ONLY Or ahtOFN_NOCHANGE DIR
    If IsMissing(varDi rectory) Then
    varDirectory = ""
    End If
    If IsMissing(varTi tleForDialog) Then
    varTitleForDial og = ""
    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 = ahtAddFilterIte m(strFilter, _
    "All Files (*.*)", "*.*")
    ' Now actually call to get the file name.
    varFileName = ahtCommonFileOp enSave( _
    OpenFile:=True, _
    InitialDir:=var Directory, _
    Filter:=strFilt er, _
    Flags:=lngFlags , _
    DialogTitle:=va rTitleForDialog )
    If Not IsNull(varFileN ame) Then
    varFileName = TrimNull(varFil eName)
    End If
    GetOpenFile = varFileName

    Forms!Form1!Fil eTxt = 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.
  • Allen Browne

    #2
    Re: Capturing File Name As Opposed To The Full File Path

    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" <cqdigital@volc anomail.com> wrote in message
    news:4d9a5a66.0 404251512.3d6c9 9d@posting.goog le.com...[color=blue]
    > 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(Opt ional varDirectory As Variant, _
    > Optional varTitleForDial og 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_FILEMUST EXIST Or _
    > ahtOFN_HIDEREAD ONLY Or ahtOFN_NOCHANGE DIR
    > If IsMissing(varDi rectory) Then
    > varDirectory = ""
    > End If
    > If IsMissing(varTi tleForDialog) Then
    > varTitleForDial og = ""
    > 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 = ahtAddFilterIte m(strFilter, _
    > "All Files (*.*)", "*.*")
    > ' Now actually call to get the file name.
    > varFileName = ahtCommonFileOp enSave( _
    > OpenFile:=True, _
    > InitialDir:=var Directory, _
    > Filter:=strFilt er, _
    > Flags:=lngFlags , _
    > DialogTitle:=va rTitleForDialog )
    > If Not IsNull(varFileN ame) Then
    > varFileName = TrimNull(varFil eName)
    > End If
    > GetOpenFile = varFileName
    >
    > Forms!Form1!Fil eTxt = 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.[/color]


    Comment

    • Wayne Aprato

      #3
      Re: Capturing File Name As Opposed To The Full File Path

      Thanks Allen. This works well.

      Comment

      • Terry Kreft

        #4
        Re: Capturing File Name As Opposed To The Full File Path

        If the file exist then Dir will return just the filename.

        i.e.
        ?Dir("C:\Docs\M yDoc.doc")
        MyDoc.doc


        --
        Terry Kreft
        MVP Microsoft Access


        "Wayne Aprato" <cqdigital@volc anomail.com> wrote in message
        news:4d9a5a66.0 404251512.3d6c9 9d@posting.goog le.com...[color=blue]
        > 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(Opt ional varDirectory As Variant, _
        > Optional varTitleForDial og 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_FILEMUST EXIST Or _
        > ahtOFN_HIDEREAD ONLY Or ahtOFN_NOCHANGE DIR
        > If IsMissing(varDi rectory) Then
        > varDirectory = ""
        > End If
        > If IsMissing(varTi tleForDialog) Then
        > varTitleForDial og = ""
        > 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 = ahtAddFilterIte m(strFilter, _
        > "All Files (*.*)", "*.*")
        > ' Now actually call to get the file name.
        > varFileName = ahtCommonFileOp enSave( _
        > OpenFile:=True, _
        > InitialDir:=var Directory, _
        > Filter:=strFilt er, _
        > Flags:=lngFlags , _
        > DialogTitle:=va rTitleForDialog )
        > If Not IsNull(varFileN ame) Then
        > varFileName = TrimNull(varFil eName)
        > End If
        > GetOpenFile = varFileName
        >
        > Forms!Form1!Fil eTxt = 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.[/color]


        Comment

        Working...