Populating a List Box with File Names

N

normad

I am using Access 97 and Windows 2000. I am trying to
populate a list box with the names of files from a
specific directory. I have have been using VBA for
several years but I can't seem to get this to work. I
have tried variations of the Dir and FileSearch, I can get
the names to print to the debug window, but getting them
into the list box is my problem. I will appreciate any
help I can get.

Thank you,

NormaD
 
E

Eric Butts [MSFT]

Hi Norma,

Try the following:

- Put the following code behind the form_open event:
Private Sub Form_Open(Cancel As Integer)

' Dimension variables.
Dim myarray()
Dim fs As Object
Dim i As Integer

' Declare filesearch object.
Set fs = Application.FileSearch

' Set folder to search.
fs.LookIn = "C:\Documents and Settings\a-fell\Desktop\"

' Set file name to search for.
fs.FileName = "*.txt"

' Execute the file search, and check to see if the file(s) are
' present.
If fs.Execute > 0 Then

' Redimension the array to the number of files found.
ReDim myarray(fs.foundfiles.Count)

' Loop through all found file names and fill the array.
For i = 1 To fs.foundfiles.Count
myarray(i) = fs.foundfiles(i)
Next i
Else
' Display message if no files were found.
MsgBox "No files were found"
End If

' Loop through the array and fill the list box on the UserForm.
If fs.foundfiles.Count > 1 Then
Me.List0.RowSource = myarray(1)
For i = 2 To fs.foundfiles.Count
Me.List0.RowSource = Me.List0.RowSource & ";" & myarray(i)
Next i
Else
For i = 1 To fs.foundfiles.Count
Me.List0.RowSource = myarray(i)
Next i
End If

' Display the UserForm.

End Sub


I hope this helps! If you have additional questions on this topic, please
respond back to this posting.


Regards,

Eric Butts
Microsoft Access Support
(e-mail address removed)
"Microsoft Security Announcement: Have you installed the patch for
Microsoft Security Bulletin MS03-026? If not Microsoft strongly advises
you to review the information at the following link regarding Microsoft
Security Bulletin MS03-026
<http://www.microsoft.com/security/security_bulletins/ms03-026.asp> and/or
to visit Windows Update at <http://windowsupdate.microsoft.com/> to install
the patch. Running the SCAN program from the Windows Update site will help
to insure you are current with all security patches, not just MS03-026."

This posting is provided "AS IS" with no warranties, and confers no rights


--------------------
| Content-Class: urn:content-classes:message
| From: "normad" <[email protected]>
| Sender: "normad" <[email protected]>
| Subject: Populating a List Box with File Names
| Date: Wed, 9 Jun 2004 15:05:02 -0700
| Lines: 12
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcRObdAJ8aCeuYL3Tu2BlZHVL8ZZ5A==
| Newsgroups: microsoft.public.access.formscoding
| Path: cpmsftngxa10.phx.gbl
| Xref: cpmsftngxa10.phx.gbl microsoft.public.access.formscoding:235329
| NNTP-Posting-Host: tk2msftngxa12.phx.gbl 10.40.1.164
| X-Tomcat-NG: microsoft.public.access.formscoding
|
| I am using Access 97 and Windows 2000. I am trying to
| populate a list box with the names of files from a
| specific directory. I have have been using VBA for
| several years but I can't seem to get this to work. I
| have tried variations of the Dir and FileSearch, I can get
| the names to print to the debug window, but getting them
| into the list box is my problem. I will appreciate any
| help I can get.
|
| Thank you,
|
| NormaD
|
 
N

NormaD

Thanks Eric, I tried this but unfortunately Access 97
running in Windows 2000 has a problem with the Filename
section of the FileSearch Object. It produces an run time
error '5', Invalid Procedure call or argument. Looks like
the DIR function is what I am going to have to use.

If you have used that function and have an insight on it,
I would appreciate it.

Thansk

Norma
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top