Solved

Populate a list box with file names for a given folder... (VB 6)

Posted on 2004-08-01
9
1,381 Views
Last Modified: 2008-01-09
Dim PathStr As String
Dim FileName As String

PathStr = "C:\MyFolder"

lstBox.Clear
.
.<code to cycle through file names in PathStr  folder to determine what and store file names in variable "FileName" >
.
lstBox.AddItem = FileName

Thanks
0
Comment
Question by:Mike Eghtebas
  • 5
  • 4
9 Comments
 
LVL 19

Expert Comment

by:Shauli
ID: 11689568
Private Sub Form_Load()
Dim myFile As String
myFile = Dir("C:\MyFolder", vbDirectory)
    Do While Not myFile = vbNullString
        lstBox.AddItem myFile
        myFile = Dir
    Loop
End Sub

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11689594
btw, maybe you want the FileListBox instead of the listbox. You'll find it in the basic components groups. To load the files into the filelistbox, all you have to do is:

Private Sub Form_Load()
     File1.Path = "C:\MyFolder"
End Sub

S
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 11689608
Short and sweet.

Could we limit them to a certain file extention?

Private Sub cboFolder_Click()                 ' Q1: or maybe cboFolder_Change()?
Dim myFile As String
myFile = Dir("C:\MyFolder", vbDirectory)
    Do While Not myFile = vbNullString
        if right(myFile,4)=".xls" Then lstBox.AddItem myFile     '<---  changed here
        myFile = Dir
    Loop
End Sub

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 11689616
Re:> filelistbox...

I will look at it.  SirBounty was talking about it too.
0
 
LVL 19

Accepted Solution

by:
Shauli earned 500 total points
ID: 11689617
Private Sub Form_Load()
Dim myFile As String
myFile = Dir("c:\MyFolder\*.xls", vbDirectory)
    Do While Not myFile = vbNullString
        List1.AddItem myFile
        myFile = Dir
    Loop
End Sub

S
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 11689625
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 11689712
Sequence of operation:
- Select a folder from cboFolder (I like to keep them in a custom cboBox because they are few and change only from time to time)
- Select a folder from cboFolder.
- Your code above is OnClick event of cboFolder and works fine.

Btw, there is an item in cboFolder called <Browse..>, when user wants to add a new folder to the list, selects <Browse..> to fire your previouse code to bringup "looking for folder" interface.

Setup for last question:  When I use <Browse..> to add a new folder to the list (in OnClick event where I have following code as well:
Dim myFile As String
myFile = Dir("c:\MyFolder\*.xls", vbDirectory)
    Do While Not myFile = vbNullString
        List1.AddItem myFile
        myFile = Dir
    Loop
End Sub

And, because the folder name is not a part of cboFolder, above code doesn't work.  If there was event like AfterUpdate of Access, it would have been fine.  

Q1:  What other event I can move the code to correct the situatiuon?

Regards,

Mike
On minor problem I have is:
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11689747
The above code doesn't work (if I get you properly) because you are looking for a folder, while the code reads: myFile = Dir("c:\MyFolder\*.xls", vbDirectory), which means that you are looking for xls files. I think yoy will have to replace the xls with *, so yoy can see all sub folders of C:\MyFolder. Otherwise you may want to take a look at the DirListBox component (again, part of the basic components). The DirListBox can work together with the FileListBox to achieve what yoy are after.

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11689753
For some reason all "you" turned out as "yoy" :)

S
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question