none
Look and select the file automatically in the folder container RRS feed

  • Question

  • Good evening:

    I' m trying without no success to select automatically a file when I open a folder.

    the code for opening a folder is easy but I don´t know how do I select the file that I am looking for in the folder.

    I`m using MSAccess 2016.  

    I show below my code for opening a folder ...

    Public Sub OpenFolder(ByVal TrackURL As String)
    Dim FolderPath As String
    FolderPath = GetFolderPath(TrackURL)
    If FolderExists(FolderPath) = True Then
        If ShellEx(FolderPath & "\") = False Then
            MsgBox "Error al abrir la carpeta contenedora.", vbExclamation, "Error al abrir carpeta contenedora"
        End If
    Else
        MsgBox "No ha sido posible abrir la carpeta contenedora puesto que ha sido eliminada, movida o renombrada. ", vbExclamation, "Error al abrir carpeta contenedora"

    End If
    End Sub

    Public Function GetFolderPath(ByVal sFullPath As String) As String
        ' Crear variables de uso
        Dim oFolder As New Scripting.FileSystemObject
        GetFolderPath = IIf(oFolder.GetParentFolderName(sFullPath) <> "", oFolder.GetParentFolderName(sFullPath), "")
    End Function

    Thanks in advance

    Thursday, September 21, 2017 3:48 PM

Answers

  • If I understand what you are looking for, I think this will do it:

    Dim FilePath As String
    
    FilePath = "C:\Your\Path\To\YourFile.doc"
    
    ' Open the file explorer to display that file in its containing folder
    Shell "explorer.exe /select," & FilePath, vbNormalFocus
    
    


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by Ixxo09 Friday, September 22, 2017 3:57 PM
    Friday, September 22, 2017 1:58 PM

All replies

  • Hi,

    What happens after you select the file? Are you saying you want to open it? If so, how do we know which file you want to open after the user selects a folder?

    Thursday, September 21, 2017 3:52 PM
  • Hi.,

    Thanks for replying.

    The only thing that I want is open the folder and show to the user the file. The user don´t select the folder, just select the file in a listbox and with a pop-up menu open the location of the file using the path.

    Figure it out that you have a lot files in a folder.  As I open the folder by the shell command I will like to show the file that the user is looking for by selecting the filename text field, like windows does, not all the files in the folder. 

    Thanks

    





    • Edited by Ixxo09 Thursday, September 21, 2017 5:00 PM
    Thursday, September 21, 2017 4:54 PM
  • Hi Ixxo09,

    from your last post what I understand that you want to select the file name from list box and only want to show that file in file dialog and don't want to show any other file.

    please refer example below.

    files in my folder.

    Code:

    Option Compare Database
    Option Explicit
    
    Private Sub Command2_Click()
    Dim fDialog As FileDialog, result As Integer
    Set fDialog = Application.FileDialog(msoFileDialogOpen)
    fDialog.AllowMultiSelect = False
    fDialog.Title = "Select a file"
    fDialog.InitialFileName = "C:\Users\v-padee\Desktop\excel files\" & List0.Value & "*"
    fDialog.Filters.Clear
    If fDialog.Show = -1 Then
       Debug.Print fDialog.SelectedItems(1)
    End If
    End Sub
    
    Private Sub Form_Load()
     Dim fileList() As String
        Dim fName As String
        Dim fPath As String
        Dim I As Integer
        fPath = "C:\Users\v-padee\Desktop\excel files\"
        fName = Dir(fPath & "*.xlsx")
        While fName <> ""
       
            I = I + 1
            ReDim Preserve fileList(1 To I)
            fileList(I) = fName
           
            fName = Dir()
        Wend
       
        If I = 0 Then
            MsgBox "No files found"
            Exit Sub
        End If
        
        For I = 1 To UBound(fileList)
            Me.List0.AddItem fileList(I)
        Next
    
    End Sub

    Output:

    Note: clicking the open button will not open the file. you need to write a code to open file.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, September 22, 2017 4:03 AM
    Moderator
  • Hi Deepak.

    I´m sorry I presumed I didn´t  explain it quite well.

    Your example is very nice and I so sorry you expended time in developing.

    I don´t kow how you insert the video.  I supposed is a gif video but I don´t know How to do it to show in a video what I want.  

    I leave two links to my dropbox in which I show you  in a gif video what I want.

    this first link is how windows do it, at the end of this video you can see the file is selected and in the second links which is my program is the same but no selected.

    I hope this could help to explain what I want.

    First link:

    https://www.dropbox.com/s/xqd4aq350pthv4t/example%20how%20windows%20does.gif?dl=0

    Second link:

    https://www.dropbox.com/s/6bz6f8gs8zg5ipi/my%20example.gif?dl=0

    Thanks in advance and sorry for all the effort of making such a nice example to show me what you think I want.




    • Edited by Ixxo09 Friday, September 22, 2017 10:48 AM
    Friday, September 22, 2017 10:33 AM
  • If I understand what you are looking for, I think this will do it:

    Dim FilePath As String
    
    FilePath = "C:\Your\Path\To\YourFile.doc"
    
    ' Open the file explorer to display that file in its containing folder
    Shell "explorer.exe /select," & FilePath, vbNormalFocus
    
    


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by Ixxo09 Friday, September 22, 2017 3:57 PM
    Friday, September 22, 2017 1:58 PM
  • Hi Dirk:

    Thank you so much  for this simple solution.  I suspected it wasn´t to difficult.  

    regards

    Joe

    Friday, September 22, 2017 3:57 PM