MS Access 2016 Import multiple CSV files

7,000

Thanks to Atzmon, the problem was in my strPath, instead of strPath = "C:\Users\xxx" it should be strPath = "C:\Users\xxx\"

Share:
7,000

Related videos on Youtube

Adre
Author by

Adre

Updated on September 18, 2022

Comments

  • Adre
    Adre almost 2 years

    I'm trying to import multiple csv files, all in the same directory into MS Access 2016.

    This is the VBA module I currently have, thanks to physics2010,but it's not working. Any tips?

    Option Compare Database
    Option Explicit
    
    Function DoImport()
    
    Dim strPathFile As String
    Dim strFile As String
    Dim strPath As String
    Dim strTable As String
    Dim blnHasFieldNames As Boolean
    
    ' Change this next line to True if the first row in CSV worksheet
    ' has field names
    blnHasFieldNames = True
    
    ' Replace C:\Documents\ with the real path to the folder that
    ' contains the CSV files
    strPath = "C:\Users\xxx"
    
    ' Replace tablename with the real name of the table into which
    ' the data are to be imported
    strTable = "Table1"
    
    strFile = Dir(strPath & "*.csv")
    
    
    Do While Len(strFile) > 0
          strTable = Left(strFile, Len(strFile) - 4)
          strPathFile = strPath & strFile
          DoCmd.TransferText acImportDelim, , 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
    
    
       MsgBox "done"
    
    
    End Function
    
    • Admin
      Admin over 7 years
      You wrote "it's not working": do you get an error message? Did you try debugging the code to see where it fails?
    • Admin
      Admin over 7 years
      It just does nothing at this stage. I must admit, I am new at Access, but I do have some coding experience. It seems as though it never gets into the While loop, as if strFile's length is never greater than 1. That's odd, because I do have multiple csv files in the directory.
    • Admin
      Admin over 7 years
      Try changing strPath = "C:\Users\xxx" to strPath = "C:\Users\xxx\" .