Populate Listbox With FileNames

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

How can I populate a listbox with a list of the filenames in a certain folder?
Can a query retrieve the filenames from a folder?

Thanks!

Mark
 
-----Original Message-----
How can I populate a listbox with a list of the filenames in a certain folder?
Can a query retrieve the filenames from a folder?

Thanks!

Mark
Hi Mark,
You must create a custom function to return the filenames
from a folder.

Try using Tools|References to set a reference to the
Office object library. Then use the Browser to lookup help
for the FileDialog library. The online help example is
useful.

Luck
Jonathan
 
You will need to use the DIR function to loop though the folder and add the
filename to a temp table or if there only a few to the source string on the
list box.
 
See the helpfile for the use of the Dir() function. Have the listbox
RowSourceType set to Value List, use Dir in a loop that builds a string of
the filename like "value1;value2;value3" and then set the Row Source to the
new string.

Mike Storr
www.veraccess.com
 
Mark said:
How can I populate a listbox with a list of the filenames in a certain folder?
Can a query retrieve the filenames from a folder?

Thanks!

Mark

A standard way might be to set your listbox ("lstFiles") to be:
Row Source Type = Value List
The code might then be:

Dim strFile As String
Dim strList As String

strFile = Dir("C:\MyStuff\*.*")

While Len(strFile) > 0
strList = strList & strFile & ";"
strFile = Dir()
Wend

Me.lstFiles.RowSource = strList


Fletcher
 
If you add the list of files to an Access table you can easily bind it to
your control:

How to Add Directory File Names to an Access Table:

Create a table named tblDirectory with 2 fields:
FileName (Text 250)
FileDate (Date/Time)

Call the code below by pressing Ctrl-G to open the debug window and type:
GetFiles("c:\windows\")

Paste this code into a regular module:

Sub GetFiles(strPath As String)
Dim rs As Recordset
Dim strFile As String, strDate As Date

'clear out existing data
CurrentDb.Execute "Delete * From tblDirectory", dbFailOnError

'open a recordset
Set rs = CurrentDb.OpenRecordset("tblDirectory", dbOpenDynaset)

'get the first filename
strFile = Dir(strPath, vbNormal)
'Loop through the balance of files
Do
'check to see if you have a filename
If strFile = "" Then
GoTo ExitHere
End If
strDate = FileDateTime(strPath & strFile)
rs.AddNew
'to save the full path using strPath & strFile
'save only the filename
rs!FileName = strFile
rs!FileDate = strDate
rs.Update

'try for next filename
strFile = Dir()
Loop

ExitHere:
Set rs = Nothing
MsgBox ("Directory list is complete.")
End Sub
 
I have a method that is slightly different from the others here. I use a
shell statement to write the entire contents of a directory to a text file,
then import the text file into a table.

On my website (see sig below) is a small sample database called
"DirectoryList.MDB" which illustrates how this is done.
 
Back
Top