Hello friends,
Searching around, I have found a great code to create a form that displays folders and files directly from MS Access. I know is simply replicating file explorer, but I would like to customize this code and having problems.
On the form I have a cbo and two list boxes. The combo displays all the drives on my machine. The 1st listbox, displays the subfolder, and the 2nd list box all the files.
I would like to display in the cbo only one drive and subfolder (which I will need to specify in the code) and perhaps use only one list box to display the files in that give drive. In addition, I would like to be able to open the files (generally are word or pdf) with the double click event.
Below is the code I have found. Thank you for any assistance.
Sub GetDrives()
' Add drives of local machine to combo box
Dim fs, dr As Variant
cboDrives.RowSourceType = "Value List"
Set fs = CreateObject("Scripting.FileSystemobject")
Set dr = fs.Drives
For Each X In dr
cboDrives.AddItem X.DriveLetter
Next
cboDrives.Value = cboDrives.ItemData(0)
End Sub
Sub GetFolders(dr As String)
' Add subfolders of drive to list box
Dim fs, fl, sf As Variant
lstFolders.RowSourceType = "Value List"
lstFolders.RowSource = ""
Set fs = CreateObject("Scripting.Filesystemobject")
'Set fl = fs.GetFolder(dr & ":\")
Set fl = fs.GetFolder(dr & ":\")
Set sf = fl.SubFolders
For Each Y In sf
lstFolders.AddItem dr & ":\" & Y.Name
Next
End Sub
Sub GetFiles(fol As String)
' Add files of subfolder to list box
Dim fs, fl, f As Variant
lstFiles.RowSourceType = "Value List"
lstFiles.RowSource = ""
Set fs = CreateObject("Scripting.FileSystemobject")
Set fl = fs.GetFolder(fol)
Set f = fl.Files
For Each X In f
lstFiles.AddItem X.Name
Next
End Sub
Searching around, I have found a great code to create a form that displays folders and files directly from MS Access. I know is simply replicating file explorer, but I would like to customize this code and having problems.
On the form I have a cbo and two list boxes. The combo displays all the drives on my machine. The 1st listbox, displays the subfolder, and the 2nd list box all the files.
I would like to display in the cbo only one drive and subfolder (which I will need to specify in the code) and perhaps use only one list box to display the files in that give drive. In addition, I would like to be able to open the files (generally are word or pdf) with the double click event.
Below is the code I have found. Thank you for any assistance.
Sub GetDrives()
' Add drives of local machine to combo box
Dim fs, dr As Variant
cboDrives.RowSourceType = "Value List"
Set fs = CreateObject("Scripting.FileSystemobject")
Set dr = fs.Drives
For Each X In dr
cboDrives.AddItem X.DriveLetter
Next
cboDrives.Value = cboDrives.ItemData(0)
End Sub
Sub GetFolders(dr As String)
' Add subfolders of drive to list box
Dim fs, fl, sf As Variant
lstFolders.RowSourceType = "Value List"
lstFolders.RowSource = ""
Set fs = CreateObject("Scripting.Filesystemobject")
'Set fl = fs.GetFolder(dr & ":\")
Set fl = fs.GetFolder(dr & ":\")
Set sf = fl.SubFolders
For Each Y In sf
lstFolders.AddItem dr & ":\" & Y.Name
Next
End Sub
Sub GetFiles(fol As String)
' Add files of subfolder to list box
Dim fs, fl, f As Variant
lstFiles.RowSourceType = "Value List"
lstFiles.RowSource = ""
Set fs = CreateObject("Scripting.FileSystemobject")
Set fl = fs.GetFolder(fol)
Set f = fl.Files
For Each X In f
lstFiles.AddItem X.Name
Next
End Sub