File Dialog - Show files only and not folders

Ace71425

Board Regular
Joined
Apr 20, 2015
Messages
130
I have this code below that opens a file dialog box and lets me select files and then writes the filename and filedatetime among other things to table...Everything works great but i'm trying to figure out how to only show files in the dialog box and not folders....In other words it should open up to the default directory of c:\ and show only pdfs, which I have working but it also shows all the folders available in c:\. Thanks!

Code:
Sub Sample()


    Dim f As Object
    Dim db      As Database
    Dim sSQL    As String
    Dim File As String
    Dim FileTime As String
    Set f = Application.FileDialog(msoFileDialogFilePicker)
    Set db = CurrentDb()
    f.AllowMultiSelect = True


    f.Filters.Clear
    f.Filters.Add "PDFS", "*.pdf"
    f.InitialFileName = "c:\"
    
    If f.Show Then
        For i = 1 To f.SelectedItems.Count
            File = Filename(f.SelectedItems(i))
            FileTime = FileDateTime(File)
                sSQL = "INSERT INTO [FaxTest] ([Rep], [Datestamp], [Fax], [Faxdate], [Timestamp]) VALUES(""" & Text0 & """, """ & Text2 & """, """ & File & """, """ & FileTime & """, """ & Text3 & """)"
                db.Execute sSQL, dbFailOnError
        Next
    End If
End Sub


Public Function Filename(ByVal strPath As String) As String
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        Filename = Filename(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function
 

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
1,808
you cant get to a file, unless there is a folder.

("The body cant exist without the mind.")
 

strive4peace

Well-known Member
Joined
Jul 6, 2009
Messages
678
adding to ranman's (what is your name) response:

By it's nature, the built-in capabilities allow you to change to other folders. If you want to exclude folders from the dialog box, you will have to build your own. You can do this by looping through the files and getting their names (and anything else you want like Size) and giving users a listbox.
 

HiTechCoach

Well-known Member
Joined
Aug 29, 2010
Messages
1,008
I have this code below that opens a file dialog box and lets me select files and then writes the filename and filedatetime among other things to table...Everything works great but i'm trying to figure out how to only show files in the dialog box and not folders....In other words it should open up to the default directory of c:\ and show only pdfs, which I have working but it also shows all the folders available in c:\. Thanks!

Code:
Sub Sample()


    Dim f As Object
    Dim db      As Database
    Dim sSQL    As String
    Dim File As String
    Dim FileTime As String
    Set f = Application.FileDialog(msoFileDialogFilePicker)
    Set db = CurrentDb()
    f.AllowMultiSelect = True


    f.Filters.Clear
    f.Filters.Add "PDFS", "*.pdf"
    f.InitialFileName = "c:\"
    
    If f.Show Then
        For i = 1 To f.SelectedItems.Count
            File = Filename(f.SelectedItems(i))
            FileTime = FileDateTime(File)
                sSQL = "INSERT INTO [FaxTest] ([Rep], [Datestamp], [Fax], [Faxdate], [Timestamp]) VALUES(""" & Text0 & """, """ & Text2 & """, """ & File & """, """ & FileTime & """, """ & Text3 & """)"
                db.Execute sSQL, dbFailOnError
        Next
    End If
End Sub


Public Function Filename(ByVal strPath As String) As String
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        Filename = Filename(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function
Are you only wanting to add files even if the user selects all which might include folders?

If yes, then I would test what type each item is before adding to your table.
 

Forum statistics

Threads
1,082,250
Messages
5,364,020
Members
400,774
Latest member
Goldi paul

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top