automatically import multiple excel files in the same folder to access 2003 table

XiiaoGerlx

New member
Local time
Today, 12:14
Joined
Mar 12, 2013
Messages
3
How can I automatically import multiple excel files in the same folder to access 2003 table??

Any ways? Thanks
 
Welcome aboard. You will need to write a code loop that reads the specified directory and links to each spreadsheet and then runs an append query. Look for FSO (file system object) to find code that will be helpful in processing files in a directory.
 
Hi there, thank you for your reply.

I am not familiar with FSO, is there any sample code for this case? I saw this code:

Code:
Public Sub RenameMoveFiles()
Const ARCHIVE_FOLDER As String = "N:\AGT\IT\Opdrachten\2012\Schatkistbankieren Aanvraagform\ExcelBestanden\Archive\"
Dim FSO As New FileSystemObject
Dim oFile As File
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim sSQL As String
Dim sAntFix As String
Dim sNewName As String
Dim sFileExt As String
sSQL = "Select ExcelFileID,ExcelFileName, ExcelFilePath, WorkbookLastModified " _
     & "From tExcelFiles " _
     & "Where Processed = -1"
Set dbs = CurrentDb
Set rs = dbs.OpenRecordset(sSQL)
With rs
    If .BOF And .EOF Then 'no files to move
        MsgBox "No files to move", vbExclamation
        .Close
        Exit Sub
    End If
    .MoveFirst
        Do Until .EOF
            sAntFix = "_" & Format(.Fields("WorkbookLastModified").Value, "yyyymmdd")
            Set oFile = FSO.GetFile(.Fields("ExcelFilePath").Value)
            sFileExt = "." & FSO.GetExtensionName(.Fields("ExcelFilePath").Value)
            sNewName = ARCHIVE_FOLDER & Replace(oFile.Name, sFileExt, sAntFix & sFileExt)
            oFile.Move (sNewName)
            .MoveNext
        Loop
        .Close
End With
Set rs = Nothing
Set dbs = Nothing
End Sub

can I use that code for my case? I'm confused. Thanks.
 
How can I automatically import multiple excel files in the same folder to access 2003 table??

Any ways? Thanks
It depends on what you mean by "automatically import multiple excel files", do you want to click a form button to import the excel in a folder or something else?
 
It depends on what you mean by "automatically import multiple excel files", do you want to click a form button to import the excel in a folder or something else?

I want to try different possible ways.
By clicking a form button to import should be the easiest way I suppose.
 
Assuming the excel files have the same format here is a snippet I always use:
Code:
Dim strFile As String
 
    DoCmd.SetWarnings False
 
'   Set file directory for files to be imported
    strPath = "C:\....\....\.....\"
'   Tell it to import all Excel files from the file directory
    strFile = Dir(strPath & "*.xls*")
 
'   Start loop
    Do While strFile <> ""
        ' Import file
        DoCmd.TransferSpreadsheet transfertype:=acImport, tablename:="Data", FileName:=strPath & strFile, HasFieldNames:=True, Range:="Template!"
        ' Loop to next file in directory
        strFile = Dir
    Loop
 
    MsgBox "All data has been imported.", vbOKOnly

This imports all excel files in the format .xls to the table "Data".
Where I have Range:="Template!" this pulls data from the template worksheet in a workbook. HasFieldNames:=True means the columns are the same in both access and excel.
 
Hi pwbrown

Assuming the excel files have the same format here is a snippet I always use:
Code:
Dim strFile As String
 
    DoCmd.SetWarnings False
 
 
    MsgBox "All data has been imported.", vbOKOnly

This imports all excel files in the format .xls to the table "Data".
Where I have Range:="Template!" this pulls data from the template worksheet in a workbook. HasFieldNames:=True means the columns are the same in both access and excel.

Range:="Template!"[/I] this pulls data from the template worksheet in a workbook. This is not clear to me ie what is the structure of template, where it is stored and what is its relationship to files stored in Directory.
Your code snippet is excellent and based on it I have already pulled a number of excel contract files in Access database. Please clarify further with example or Illustration. Thanks
 

Users who are viewing this thread

Back
Top Bottom