Accessing VBA - while importing * .CSV I am getting runtime error 31519. You cannot import this file

When using the DoCmd.TransferText method in Access, I am getting the error described in the title of the question. My code takes the filenames from a table in the current database (these are files selected from over 2000 files present in the same folder) and its purpose is to import the contents of those files into a single table in Access. Please read this thread for more information: VBA procedure to import only selected csv files (from one folder) into one table on access

All files have the same structure and data type. enter image description here

here is the code i am using:

 Sub Importing_data_into_a_single_table_csv_version()
   Dim my_path As String
   Dim rs As Recordset
   Dim start As Double
   Dim total_time As String

     DoCmd.SetWarnings True
     my_path = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl_csv\"     ' source folder.

     start = Timer         ' remember time when macro starts.

     Set rs = CurrentDb.OpenRecordset("CSV Files")   ' opens the table with file names.
       Do Until rs.EOF
          If Dir(my_path & rs.Fields(0).Value) <> "" Then
          ' DoCmd.TransferText acImportDelim, "macro_link_specification", "all_stocks_3", "my_path & rs.Fields(0).Value", True
             DoCmd.TransferText acImportDelim, "import", "all_stocks_1", "my_path & rs.Fields(0).Value", True
          ' expression. TransferText ( TransferType, SpecificationName, TableName, FileName, HasFieldNames, HTMLTableName, CodePage )

          End If
          rs.MoveNext
       Loop

   total_time = Format(Timer - start, "hh:mm:ss")
   MsgBox "This code ran successfully in " & total_time, vbInformation
 End Sub

      

Code crashes with DoCmd.TransferText acImportDelim, "import", "all_stocks_1", "my_path & rs.Fields(0).Value", True

.

enter image description here Is there some special way to have the destination table prepared to import these files? Sorry for such questions, but I've been using Access for 3 weeks now, so I'm just a beginner. I am using an import specification called "import". Could this be the cause of the problems?

This is what the destination table looks like: enter image description here

Here is another table I was trying to import my data into. The field names in this table do not have special characters and the same data types specified in the fields, but this does not make any difference. The same error 31519 is returned.

enter image description here

+3


source to share


1 answer


The solution turned out to be simple:

DoCmd.TransferText acImportDelim, "import", "all_stocks_1", _
                   "my_path & rs.Fields(0).Value", True

      

has filename variables inside quotes, so it was used as a literal string (and gave an invalid path).



Right:

DoCmd.TransferText acImportDelim, "import", "all_stocks_1", _
                   my_path & rs.Fields(0).Value, True

      

+1


source







All Articles