Solved Only the Document, not the Path

Yes. That's after using the code You posted.
That's not what I get when I try it. Can you tell us the following properties?

Column Count
Bound Column
Column Widths
 
@sergio vieira Your requirement is that the listbox display of files needs to show just the filename and not the path.
You have a field in some table that holds the path to the document, which, because it is the path, incorporates the filename.
Presumably you get the path using a filesystem dialog to pick the file you want to specify in the link to the record.
From a database design view you have two items of data: the link to the file and the filename. Your table design should reflect that difference. It will also give you the ability to edit and give the "filename" in the record a name which is meaningful to end users (or to leave as is) without altering the link to the file.
As @Pat Hartman indicated (Post #3): "Either create a function to extract only the file name or add a second column so you can save the values in two fields from the beginning which is the best solution."
The table structure - which in this case relates documents to specific person records, allows classification of docs to doc types and assigns a date could look like this:
1743895899486.png

Which could be maintained using a form such as
1743896373286.png


The {SELECT} button opens a file dialog to select the file. The VB for this button uses
sFile = FSBrowse("", msoFileDialogFilePicker, "All Files (*.*),*.*")
to get the path which can be assigned to strDocPath and
Me.txtDocName = GetFileName(sFile)
to default the name of the file to the Document name

The Document name can then be used to open the file on the dbl_click event (not single click - as that allows you to edit the name of the document to whatever you want.)
Call ExecuteFile(Me.txtDocLink, "Open")

Your listbox will need to present the Doc Name from the table and the event will use the open action using DocLink

The functions (placed in a VBA module) are based on Daniel Pineault's blog posts:
https://www.devhut.net/vba-extract-the-file-name-from-a-complete-file-path-and-name/
and this
https://www.devhut.net/late-binding-the-filedialog/
 
Last edited:
to get filename from Path:
Code:
Function LastElement(StrIN As String, Optional Separator As String = "\") As String
    Dim var
    var = Split(StrIN, Separator)
    LastElement = var(UBound(var))
End Function
 
to get filename from Path:
Code:
Function LastElement(StrIN As String, Optional Separator As String = "\") As String
    Dim var
    var = Split(StrIN, Separator)
    LastElement = var(UBound(var))
End Function
Well I thought the Dir() from ChatGPT was the easiest, but I cannot get it to work? :(
 
Dear Friend theDBguy. It's working perfectly. Please accepts my sincere apologies for the trouble I gave youy, but there was one extra line in the previous code and the result was the same. Now everything is working as shown in the image. Many thanks to you and all the colleagues here on the forum who intervened in this post. Hugs from Portugal. Solved

1743927803582.png
 
Which is why Dir() is a useful to quickly test the existence of a file:
Code:
FileExists = Len(Dir(strPathToFile)) > 0
(y)

... and why folk who believe ChatGPT actually knows or understands anything are stupid!

It can talk the talk, but rarely can it walk the walk 😖
 
To the helpers who are now using ChatGP, et all to find answers. ALWAYS test thoroughly any suggestion by an AI BEFORE posting it, please. Don't just take the answer as gospel.
 

Users who are viewing this thread

Back
Top Bottom