2
votes

I'd like to import all Excel files (with different data and columns) from some directory into MS Access 2010 database, creating new table for each file. I've found the code to import files into one table:

Option Compare Database
Option Explicit

Function DoImport() 

Dim strPathFile As String, strFile As String, strPath As String
 Dim strTable As String
 Dim blnHasFieldNames As Boolean

 ' Change this next line to True if the first row in EXCEL worksheet
 ' has field names
 blnHasFieldNames = True

 ' Replace C:\Documents\ with the real path to the folder that
 ' contains the EXCEL files
 strPath = "C:\Documents and Settings\myName\My Documents\Access Test\"

 ' Replace tablename with the real name of the table into which
 ' the data are to be imported
 strTable = "tablename"

 strFile = Dir(strPath & "*.xls")
 Do While Len(strFile) > 0
       strPathFile = strPath & strFile
       DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
             strTable, strPathFile, blnHasFieldNames

 ' Uncomment out the next code step if you want to delete the
 ' EXCEL file after it's been imported
 '       Kill strPathFile

       strFile = Dir()
 Loop

End Function

But I need to create new table each time. Is it possible in VBA?

3
re: "I would be so thankful if someone can provide step by step example on creating buttons, modules and running." - Wow. If that isn't "too broad" I don't know what is.Gord Thompson
I've been surfing web for long enough, but have not found examples on how to create button and link it with module. I believe it won't take long to give a quick guide. Anyways, my main problem is about creating new tables in loop.Gyuzal R
How do you want the new tables named?HansUp
@HansUp, according to the filenames.Gyuzal R

3 Answers

3
votes

I think all you need to do is change the destination table name (the value of strTable) each time before you do DoCmd.TransferSpreadsheet.

In a comment you said you want the table name to be derived from the workbook file name. And, each time through your loop, another variable (strFile) contains the file name. So I think you could strip the file extension from that file name and use it as the Access table name.

Here is an Immediate window example which demonstrate how that can be done ...

strFile = "foo.xls"
strTable = Left(strFile, Len(strFile) - 4)
? strTable
foo

If that approach is suitable, revise the loop in your VBA code like this (untested) code snippet ...

strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
    strPathFile = strPath & strFile
    strTable = Left(strFile, Len(strFile) - 4)
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
        strTable, strPathFile, blnHasFieldNames
    strFile = Dir()
Loop
1
votes

I used to be a MOS Access 2003. Now everyone is using 2010 but many things have not changed.

When you do a manual import or export, you can save the layout as a specification.

This process can be automated by a macro.

Check out the link below for more details and steps.

http://office.microsoft.com/en-us/access-help/run-a-saved-import-or-export-operation-HA001226020.aspx?CTT=5&origin=HA001226307

As for the other stuff, buttons, modules, etc, please read the on line help / documentation first.

We are here to help but not do the work for you.

J

1
votes

Okay, I do not know if it is an issue with my computer not being on the current office CU.

http://msdn.microsoft.com/en-us/library/office/ff192475(v=office.14).aspx

Here is a link to how to use the ImportExport Macro. Use to be in the macro section.

I did read that you had to trust the location. So I tried both my location c:\msdn plus the default for the wizards.

enter image description here

Still was not able have it the option come up.

I tried creating a specification to see if one was needed for the option to show, no dice.

However, there is a DoCmd.TransferText and DoCmd.TransferSpreadSheet.

Both will allow you to import.

Create a function. Call the function from a macro (RunCode). Another way is to create a main menu form. Have a button. On the click command, run the code.

Please tell me if you ever get the ImportExportData Macro to show. I think it is a bug. I will need to bring down the latest Cumulative Updates and try again.

enter image description here