?
Solved

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

Posted on 2004-08-01
9
Medium Priority
?
1,392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Suggested Courses

719 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