Results 1 to 8 of 8
  1. #1
    shaka is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2012
    Posts
    4

    Importing From Multiple Excel Files Problem


    Hi Folks

    I am trying to import data from multiple Excel files saved in the one folder to a single table in Access.

    I found this macro on the web and I think I am nearly there but when I run the macro it returns a fault in the code here

    Code:
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strPathFile, blnHasFieldNames
    full code I am using

    Code:
    Option Compare Database
    Option Explicit
    
    Public Function ImportExcelMultipleFiles()
    
    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 = False
    
    ' Replace C:\Documents\ with the real path to the folder that
    ' contains the EXCEL files
    strPath = "C:\Users\Kevin\Google Drive\ATOS\ATOS Reports\"
    
    ' Replace tablename with the real name of the table into which
    ' the data are to be imported
    strTable = "ATOSData"
    
    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

  2. #2
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    What type of Excel file are your trying to import. Your VBA code indicates that the file type is Excel 97-2003 with an .xls extension. Is this correct?

  3. #3
    shaka is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2012
    Posts
    4
    Yes still using 2003 at the moment till we get the long awaited upgrade in work

  4. #4
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Also, you have indicated that you have no field names (column headers) in your excel file. Is this also true? Can't see anything else in this line of code. What error message do you get?

    Just had a thought, delete this section of the code. acSpreadsheetTypeExcel9

  5. #5
    shaka is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2012
    Posts
    4
    I do have headers and have now changed that to True

    Deleted that section but when I run the macro it still pops up my code with the following line highlighted in yellow

    Code:
    DoCmd.TransferSpreadsheet acImport, strTable, strPathFile, blnHasFieldNames

  6. #6
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Change that line of code to

    DoCmd.Transferspreadsheet acImport, , strTable, strPathFile, blnHasFieldNames

    Where you had the File type, you need for the code to know that it is a wildcard, by leaving it blank and not ignoring it completely. With what you did, VBA thought the file type was the next string (strTable) and therefore bombed. If you don't use an option section of the code, you must supply the comma so that it is instructed to move to the next item.

    Here is a link to DoCmd.Transferspreadsheet syntax.

    http://msdn.microsoft.com/en-us/libr...ffice.12).aspx

    Alan

  7. #7
    shaka is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2012
    Posts
    4
    All working now thank you Alan for taking the time to help with this

  8. #8
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Glad to have helped.

    Alan

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 22
    Last Post: 12-29-2015, 10:41 PM
  2. Replies: 6
    Last Post: 12-03-2012, 08:08 AM
  3. Linking multiple Excel files - PLEASE help!
    By studor63 in forum Import/Export Data
    Replies: 4
    Last Post: 09-11-2012, 01:24 PM
  4. Replies: 4
    Last Post: 06-14-2011, 07:19 PM
  5. Importing multiple files at once
    By NoiCe in forum Import/Export Data
    Replies: 1
    Last Post: 04-01-2009, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums