populate a table with files in a folder

B

Bonnie

Hi,

I know I have done this before but I can't remember or find the function. I
need to populate a table with the names of files in a given folder.

Any ideas?

Thanks in advance.

Bonnie
 
D

Douglas J. Steele

Assuming your table is named Files, with a field FileName in it, the
following will put the name of the files into that table:

Dim strFile As String
Dim strFolder As String
Dim strSQL As String

strFolder = "C:\MyFolder\"
strFile = Dir$(strFolder & "*.*")
Do While Len(strFile) > 0
strSQL = "INSERT INTO Files (FileName) " & _
"VALUES(" & Chr$(34) & strFile & Chr$(34) & ")"
CurrentDb.Execute strSQL, dbFailOnError
strFile = Dir$()
Loop

If you want the full path to the file, use

strSQL = "INSERT INTO Files (FileName) " & _
"VALUES(" & Chr$(34) & strFolder & strFile & Chr$(34) & ")"
 
Top