
Populate list box with names of folders on a selected disk
One of the examples in the Help file entry for Dir illustrates:
' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display entry only if it
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop
However, rather than all the work you're going to, why not simply use the
GetOpenFileName API call, as illustrated in
http://www.mvps.org/access/api/api0001.htm at "The Access Web"?
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
Quote:
> Group:
> I want to populate a list box on a form with the names of all folders that
> are on a selected volume (drive) that the user has selected. I have found
> code that will list all files (for example MyDatabase.mdb) on a particular
> volume selected by the user, but I can't figure out how to change the code
> to list the file folders. My form has a combo box where the user selects a
> drive. The change event for the combo box then populates the list box
with
> all files in the root directory, if any, but not the folders.
> 'Me.CompanyName is the list box object where I want the name of the
Folders
> to be listed.
> 'Me.BackupDestination is the combo box object that lists the available
> volumes (drives). If the user selects the A drive, Me.Destination="A:\"
> Here is my existing code:
> 'DRIVE LIST BOX - ON CHANGE EVENT
> Private Sub BackupDestination_Change()
> IntCount = 0 ' reset to empty array used int DirListBox
> Me.CompanyName.RowSourceType = "DirListBox"
> End Sub
> 'POPULATE THE LIST BOX WITH AVAILABLE FILES FROM RESTORE LOCATION
> Function DirListBox(fld As Control, ID, row, col, code)
> Dim StrFileName As String
> Static StrFiles(0 To 511) As String 'Array to hold file names
> 'IntCount is dimensioned as Module Variable
> Select Case code
> Case 0
> DirListBox = True
> Case 1
> DirListBox = Timer
> StrFileName = Dir$(Me.BackupDestination)
> Do While Len(StrFileName) > 0
> StrFiles(IntCount) = StrFileName
> StrFileName = Dir
> IntCount = IntCount + 1
> Loop
> Case 3
> DirListBox = IntCount
> Case 4
> DirListBox = 1
> Case 5
> DirListBox = 1440
> Case 6
> DirListBox = StrFiles(row)
> End Select
> End Function
> Any help would be greatly appreciated.
> Thanks,
> BobV