Solved

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

Posted on 2004-08-01
9
1,377 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
Comment Utility
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
Comment Utility
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 33

Author Comment

by:Mike Eghtebas
Comment Utility
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
 
LVL 33

Author Comment

by:Mike Eghtebas
Comment Utility
Re:> filelistbox...

I will look at it.  SirBounty was talking about it too.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 19

Accepted Solution

by:
Shauli earned 500 total points
Comment Utility
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 33

Author Comment

by:Mike Eghtebas
Comment Utility
0
 
LVL 33

Author Comment

by:Mike Eghtebas
Comment Utility
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
Comment Utility
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
Comment Utility
For some reason all "you" turned out as "yoy" :)

S
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now