Solved

Import multiple Excel files into Access

Posted on 2009-07-13
4
632 Views
Last Modified: 2013-11-27
I have a bunch of individual Excel files. They are all contained in the same folder. Each one only has a few records. They all have the same format (data in the first 5 columns only). I'm looking for an import program that will import all the files in the folder so that I don't have to manually do a "get external data" for each individual file since there are like a 100 of them and I will be having to do this task frequently in the future.  Or what also would be fine is a macro to combine all of the excel files into one big file. Either way is fine.... a program in Access or a program in Excel. Thanks.
0
Comment
Question by:dbfromnewjersey
  • 2
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 24842897
Hello dbfromnewjersey,

Assuming you want all Excel files in a given folder...

Dim fso As Object, fld As Object, fil As Object

Set fso = CreateObject("Scripting.FIleSystemObject
Set fld = fso.GetFolder("c:\folder\subfolder")

For Each fil In fld.Files
    If LCase(Right(fil.Name, 4)) = ".xls" Then
        DoCmd.TransferSpreadsheet TransferType:=acImport, _
            TableName:= "NameOfTable", FileName:=fil.Path, _
            HasFieldNames:=True
    End If
Next

Set fil = Nothing
Set fld = Nothing
Set fso = Nothing

Regards,

Patrick
0
 

Author Comment

by:dbfromnewjersey
ID: 24845272
I put that code under a command button on a form, changed the to the right of GetFolder and changed TableName to my table name. When I run the code, nothing happens. Any thoughts?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24845304
dbfromnewjersey,

Please paste back the code exactly as you implemented it.

Regards,

Patrick
0
 

Author Comment

by:dbfromnewjersey
ID: 24845414
Now I'm getting an error saying: "Run-time error 2391".  Field 'F1' doesn't exist in destination table tblTEST.  I should mention that my excel files do not have field names/headers. The data starts on row 1 of each file. There is nothing in the folder other than the files I need to import. I also tried commenting out the "If Then" logic since the only things in the folder are excel files. Here is the code under the command button:

Private Sub Command0_Click()
Dim fso As Object, fld As Object, fil As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder("F:\TESTFOLDER")
For Each fil In fld.Files
    If LCase(Right(fil.Name, 4)) = ".xls" Then
        DoCmd.TransferSpreadsheet TransferType:=acImport, _
            TableName:="tblTEST", FileName:=fil.Path, _
            HasFieldNames:=False
    End If
Next

Set fil = Nothing
Set fld = Nothing
Set fso = Nothing

End Sub
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question