Display folders and files (1 Viewer)

JPR

Registered User.
Local time
Today, 13:48
Joined
Jan 23, 2009
Messages
192
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:48
Joined
May 7, 2009
Messages
19,169
on the demo, close form1 and open in design view.
click on the combobox.
on it's property sheet->data->row source, put the drive and the folder there (without backslash to the right).
see other codes in the form.
run the form.
 

Attachments

  • drives and folders.zip
    26.9 KB · Views: 425

JPR

Registered User.
Local time
Today, 13:48
Joined
Jan 23, 2009
Messages
192
Thank you Arnel. I have made the change to the cbo row source as recommended (C:\Folder) but getting an error message:
The expression After Update you entered as the event property setting produced the following error: Variable not defined.

Thank you
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:48
Joined
May 7, 2009
Messages
19,169
sorry about that, i change the declaration of Sub Getfiles().
replace your sub with this:
Code:
Sub GetFiles(ByVal fol As String, ByRef lstFiles As ListBox)

' Add files of subfolder to list box

Dim fs, fl, x, 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
 

JPR

Registered User.
Local time
Today, 13:48
Joined
Jan 23, 2009
Messages
192
Fantastic Arnel. Great job as always. Thank you
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:48
Joined
May 7, 2009
Messages
19,169
you're welcome!:)
 

Users who are viewing this thread

Top Bottom