Here a modified it for you:
Function GetDirListing(strPath As String, Optional strFilter As String)
' Procedure : GetDirListing
' Author : CARDA Consultants Inc.
' Website : http:\\
www.cardaconsultants.com
' Purpose : List all the files within a directory
' Copyright : The following may be altered and reused as you wish so long as
the
' copyright notice is left unchanged (including Author, Website
and
' Copyright). It may not be sold/resold or reposted on other
sites (links
' back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' strPath = full path include trailing \ ie:"c:\windows\"
' strFilter = extension of files ie:"pdf". if you want to return
' a complete listing of all the files enter a value of
' "*" as the strFilter
'
Dim MyFile As String
Dim MyFolder As String
Dim Db As Database
Dim rs As Recordset
Dim tblName As String
Dim tblFilenameField As String
'On Error GoTo Error_Handler
tblName = "MyMusicFiles" 'Table name to add files listing to
tblFilenameField = "FileName" 'Field in the table for the filename
Set Db = CurrentDb
Set rs = Db.OpenRecordset(tblName)
MyFolder = strPath
'Add the trailing \ if it was omitted
If Right(MyFolder, 1) <> "\" Then MyFolder = MyFolder & "\"
'Modify the strFilter to include all files if omitted in the function
'call
If strFilter = "" Then strFilter = "*"
'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$(MyFolder & "*." & strFilter, vbNormal + vbHidden + vbSystem)
Do While MyFile <> ""
'FileCopy SourceFile, DestinationFile
rs.AddNew
rs(tblFilenameField) = MyFile
rs.Update
MyFile = Dir$
Loop
rs.Close
Set rs = Nothing
Set Db = Nothing
Exit Function
Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: GetDirListing" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Exit Function
End Function
you'd run it by using a command like
GetDirListing("C:\Documents and Settings\All Users\Documents\My Music\Sample
Music")
Don't forget to switch the
tblName
tblFilenameField
variables to suit your table name and structure!
--
Hope this helps,
Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples:
http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.