Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-01
9
Medium Priority
?
1,398 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
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

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.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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.
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…
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…
Suggested Courses

783 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