0

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
DavidPostill
  • 153,128
  • 77
  • 353
  • 394
Adre
  • 11
  • 2
  • 5
  • You wrote "it's not working": do you get an error message? Did you try debugging the code to see where it fails? – Atzmon Oct 06 '16 at 06:54
  • 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. – Adre Oct 06 '16 at 06:58
  • Try changing `strPath = "C:\Users\xxx"` to `strPath = "C:\Users\xxx\"` . – Atzmon Oct 06 '16 at 07:52

2 Answers2

0

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

Adre
  • 11
  • 2
  • 5
0

Is this working for you to import into one data file? The code works, but it imports each file as a separate database every time I do it.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 17 '22 at 19:43