Home  Fees/Services  Access 2010 Template  Access Tutorial  Access Download  Articles  Search  Contact  Privacy  Links

Directory List Box
Folder List Box

Visual Basic Tutorials:
Access-Google Earth
Age Calculation
Change To Proper Case
Email via Gmail #1
Send Gmail Email #2
Inactivity Logout Code
Outlook Email
Read Email Access
Email Attachment
Send Outlook Email
Running Sum
Denormalize Records
Stock Quotes
Find Database Path
Detail-Master Update
Data Field Validation
Field Value New-Old
Access Version
Global Variable Parameter
Global Variables
Active Labels
Files List Box
Mail Merge
Quick Sort
Recordset Filters
Reference Form Field
Select Case
Access Transactions

Visual Basic Function Examples

File List Box

If you need to display a directory listing you can fill a list box with each folder or file name. There are three parts needed to accomplish this, a list box control, the VBA Dir$ function, and an array. To begin you will pull each directory value into the array. Next you will need to sort the array to present the information in a clear manner. Last you will place the directory information into the list box control. The routine below requires two parameters, the list box control and a string containing the path to the directory you need to display.

Since 2000 we have developed nearly 500 databases for clients throughout the world. See a sample of them at our recent clients page.

Do you and your clients need access to data on the Internet? We can create a secure system to match your requirements perfectly.

To pull all the values in the directory you pull the first file name with

    file_name = Dir$(strDrive & "\*.*", vbDirectory)

The parameter vbDirectory indicates that you are looking at a directory. The value is placed in the array and then each subsequent value is requested using

    file_name = Dir$()

When all values have been placed in the array this routine calls a quick sort routine to sort the values. Last, the sorted list is placed in the list box control by setting its row source property to the values from the sorted array.

Public Sub FillList(cmbList As ListBox, strDrive As String)

Const ArrTop As Integer = 300 ' limit to 300 files, and that is way too many
' file list box row source property is limited to
' 2048 characters so at 300 files that is
' about 7 characters per name
Dim MyArray(ArrTop) As String

Dim file_name As String
Dim strRowSource As String ' Build row source string here to get all files
Dim i As Integer ' MyArray index for fill
Dim j As Integer ' MyArray index for move to list box

On Error Resume Next
'list the names of all files in the specified directory

file_name = Dir$(strDrive & "\*.*", vbDirectory)
i = 1
Do While (Len(file_name) > 0) And (i < 101)

    ' See if we should skip this file.
    If Not (file_name = ".") Or (file_name = "..") Then
        MyArray(i) = file_name
        i = i + 1
    End If

    ' Get the next file.
    file_name = Dir$()

If i > 1 Then

    ' note here is the code for QuickSort subroutine
    QuickSort MyArray, 1, i - 1
    'use sorted array to refill listbox
    cmbList.RowSource = "" 'clears the list
    For j = 1 To i
        strRowSource = strRowSource & MyArray(j) & ";"
    Next j

        ' If there are too many files take the most recent
        'RowSource string length is 2048 characters
        cmbList.RowSource = Left(strRowSource, 2048
    cmbList.RowSource = MyArray(1)
End If

End Sub

Sample Directory
file list box

List Box Control

directory list box


Blue Claw Database Design Downloadable Tutorial:
How To Program Continuous-Continuous Master/Detail Forms  

A Blue Claw Software Design Template:

Customer Order Fullfillment Database

A Blue Claw Database Design Article:

MS Access Versus Other Systems

Microsoft Office:
MS Access 2000 Through 2016 and Office 365 & Sharepoint

Contact Information

Copyright 2000-2018 Blue Claw Database Design

Microsoft Access 2007, 2010, 2013 & 2016