Question : import file lising into Access with VB

should be fun for someone... i want to click a button on my Access form and have the listing of file names of the files from a certain folder imported into an an MS Access table.  Not the contents of the file but merely the name of the file.  Getting date created to come along with it into a different column would also be nice if anyone want to take a crack at that.

Answer : import file lising into Access with VB

Need references to DAO and Scripting Runtime

Public Function GetFileNameAndDate()
On Error GoTo Err_GetFileNameAndDate
 
    Dim objFSO As New FileSystemObject
    Dim objFolder As Folder
    Dim objFile As File
    Dim rs As DAO.Recordset
    Dim strFilePathName As String, strSrcFileDir As String
    Dim strLocalTable As String, strMsg As String
   
        'local table we want to populate with values from Excel files
    strLocalTable = "FileNameAndDate"
   
        'file directory folder we want to retrieve file names from
    strSrcFileDir = "c:\temp"
   
        'open local table for writing
    Set rs = CurrentDb.OpenRecordset(strLocalTable)

        'access the specified folder object
    Set objFolder = objFSO.GetFolder(strSrcFileDir)
   
        'notify user to be patient
    strMsg = "There are " & objFolder.Files.Count & " files in the selected folder."
    strMsg = strMsg & vbCrLf & "Processing may take some time.  Please be patient."
    MsgBox strMsg, , "Notice"
   
        'iterate through files in the directory and count Excel files
    For Each objFile In objFolder.Files
        rs.AddNew
                'write file name and date created to local table record
            rs!FileName = objFile.name
            rs!FileCreateDate = objFile.DateCreated
        rs.Update
        'move to next file in folder
    Next
 
Exit_GetFileNameAndDate:
        'clear object variables
    Set objFile = Nothing
    Set objFolder = Nothing
    Set objFSO = Nothing
    Set rs = Nothing
    Exit Function
   
Err_GetFileNameAndDate:
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_GetFileNameAndDate
   
End Function

OM Gang
Random Solutions  
 
programming4us programming4us