Populate list box with names of folders on a selected disk 
Author Message
 Populate list box with names of folders on a selected disk

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



Thu, 03 Mar 2005 01:00:00 GMT  
 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



Thu, 03 Mar 2005 01:52:42 GMT  
 Populate list box with names of folders on a selected disk
Doug:

Thank you for your response.

From the code that you suggested I found my answer.  I changed one line of
my code and it does exactly what I want! All I had to do was to add
"vbDirectory" to the Dir$ function as shown below and now it displays
directories (folders). Thanks very much for your help. I don't think I would
have found the answer without your help.

StrFileName = Dir$(Me.BackupDestination, vbDirectory)

Thanks,
BobV



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



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



Thu, 03 Mar 2005 02:48:53 GMT  
 Populate list box with names of folders on a selected disk
Glad you got it working. I still stand by my original comment, though. In my
opinion, you'll be building a better UI if you use the standard Windows File
Open dialog, rather than building your own home-grown equivalent.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele


Quote:
> Doug:

> Thank you for your response.

> From the code that you suggested I found my answer.  I changed one line of
> my code and it does exactly what I want! All I had to do was to add
> "vbDirectory" to the Dir$ function as shown below and now it displays
> directories (folders). Thanks very much for your help. I don't think I
would
> have found the answer without your help.

> StrFileName = Dir$(Me.BackupDestination, vbDirectory)

> Thanks,
> BobV



> > One of the examples in the Help file entry for Dir illustrates:

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



Thu, 03 Mar 2005 04:20:33 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Beginner: Populating text box by selecting from list box

2. How to list macro names in a combo box or a list box

3. Populate List Box with Large List

4. Use list from excel or access to populate combo/list box

5. Populating a List Box with a large list

6. Rebuild select list for combo/list box

7. Updating a combo box list W/limit to list selected

8. How to populate a "select list"

9. Populate a Select drop down list at runtime in DHTML app

10. select item in list, populate dbgrid

11. Populate Combo Box With Files in Current Folder

12. Determine Selected Folder and Selected Item in Folder

 

 
Powered by phpBB® Forum Software