VBA example - Microsoft Access:VBA display files of a folder

VBA display files of a folder



In previous example, you had learned VBA code to display subfolders in a folder by using subfolders property. For this example, you will learn VBA code to display files in a folder by using Drives, Subfolders, and Files properties. All files in a folder will  be populated in the the list box when the folder is double-clicked. The example illustrates a FileFolder form that has a combo box(CboDrives), two list boxes(LstFolders,LstFiles). See the figure below:

 VBA example - Microsoft Access VBA Show Files Folder Form View 

-When the form loads, the drive C is selected and its subfolders populated in the list box.(See FileFolder form above).

- When you double-click the folder in the folder list(left list box), all files that contain in the folder will display in the file list(right list box).

-If you want to select any drive, click arrow down of combo box and select the drive.

To have a form as the figure above, you have to create the form in Form Design. See the figure below:

VBA example - Microsoft Access VBA Show Files Folder Form Design 

- Drag and drop a combo box, and two list boxes to the form.

- Set Name properties of the three controls:

Name: CboDirectory

Name: LstFolers

Name: LstFiles

 

Then apply the VBA Code below:

  

Private Sub Form_Load()

    CboDrives.RowSource = ""

    GetDrives

    GetFolders (CboDrives.Value)

End Sub

 ............................................................................................

Private Sub CboDrives_Change()

    GetFolders (CboDrives.Value)

End Sub

 ............................................................................................

Private Sub LstFolders_DblClick(Cancel As Integer)

    GetFiles (LstFolders.ItemData(LstFolders.ListIndex))

End Sub

 ............................................................................................

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 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

 ............................................................................................

Sub ShowFileList(folderspec)

' Show files of subfolder in the list box

    Dim fs, fl, f As Variant

    Set fs = CreateObject("Scripting.FileSystemObject")

    Set fl = fs.GetFolder(folderspec)

    Set f = fl.Files

    For Each z In f

      LstFiles.AddItem z.Name

    Next

End Sub




Comments

Name:
Comment:
Enter the text:
CAPTCHA image

.........................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word.
However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like