问题
I am having a super hard time trying to import multiple Excel files into a specific Access 2013 table.
I tried using the code I found from another SO question:
Do While Len(strFile) > 0
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, strPathFile, blnHasFieldNames
Loop
but this makes new tables for each file, and I need all 101 files to be in a single table. The code I try to test it fails on DoCmd.TransferSpreadsheet, and I can't seem to get that part to work at all.
回答1:
I have had luck using the command with these parameters:
DoCmd.TransferSpreadsheet 0, 10, tableName, wbPath, True, "MySheet!A1:C100"
The line is copy-pasted from a working application, where data from an Excel-file is imported to an existing Access table without overwriting or changing the existing data in the table.
Explanation of the parameter list:
- 0: should be the same as the acImport enum
- 10: is the same as the acSpreadsheetTypeExcel12Xml enum (I think I used the integer 10 because of some compatibility, where the enum had different values or were undefined in some versions of Access)
- tableName: is the name of an existing table in your Access database
- wbPath: is the absolute file path to a workbook saved in the xlsx format
- True: indicates that the sheet has headers
- "MySheet!A1:C100" indicates the sheet name and the range of the cells that should be imported.
I cannot remember if the column headers and/or the order of the columns in the Excel sheet have to match the Access table columns. But it would certainly make sense that they should match. Have you checked that?
I recall that if some of the datatypes in the cells in the Excel-sheet did not match/fit the datatype of the corresponding column in the Access table, Access would create some temporary "report tables", which indicated that an error had happened during the import. Maybe that is what you're experiencing?
Here is the list of file type enums (second function argument) and their corresponding file types. Are you using the correct enum value? I see you provide a value of acSpreadsheetTypeExcel9, are you importing from Excel 2000 files?
Excel 3 acSpreadsheetTypeExcel3
Excel 95 acSpreadsheetTypeExcel7
Excel 97 acSpreadsheetTypeExcel8
Excel 2000 acSpreadsheetTypeExcel9 (default)
Excel 2002 acSpreadsheetTypeExcel10
Excel 2003 acSpreadsheetTypeExcel11
Excel 2007 Binary Format (.xslb) acSpreadsheetTypeExcel12
Excel 2007 (xlsx) acSpreadsheetTypeExcel12Xml
Update:
Access will use the table in the current database, that you run the macro from (assuming you run it from within Access). If you want to import to a another database, you can do this:
Dim accessApplication As Variant
Set accessApplication = CreateObject("Access.Application")
accessApplication.OpenCurrentDatabase databasePath, False
accessApplication.DoCmd.TransferSpreadsheet 0, 10, tableName, wbPath, True, "MySheet!A1:C100"
Where databasePath is the absolute file path to an existing database. The last parameter specifices whether you want to open the database in exclusive mode. The default value is False, which specifies that the database should be opened in shared mode.
The above code will make sure that the data is imported into the specified table in a specific Access database file.
If all the data you wish to import is stored in one Excel-file, I would recommend that you put the import macro in the Excel-file instead of having it in multiple Access files. That makes it easier to maintain the macro. Remember to add a reference to the Microsoft Access Object Library in Tools -> References in the VBA editor. If you both have multiple Excel-files and multiple Access files, you could consider having some master Excel-workbook,which just holds the macro and then opens the other Excel-files and Access databases.
来源:https://stackoverflow.com/questions/32510984/import-entire-folder-of-excel-files-into-access-2013