csv - Access VBA - 导入 *.CSV 时出现运行时错误 31519。您无法导入此文件

标签 csv ms-access vba

在 Access 中使用 DoCmd.TransferText 方法时,我收到问题标题中描述的错误。
我的代码正在从当前数据库中的表中获取文件名(这些文件是从单个文件夹中存在的 2000 多个文件中选择的文件),其目标是将这些文件的内容导入到 Access 中的一个表中。
请阅读此主题以获取更多信息:
VBA procedure to import only selected csv files (from one folder) into a single table in access

所有文件都具有相同的结构和数据类型。 enter image description here

这是我正在使用的代码:

 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

代码在 DoCmd.TransferText acImportDelim, "import", "all_stocks_1", "my_path & rs.Fields(0).Value", True 崩溃.

enter image description here
目标表是否应该准备一些特殊的方式来导入这些文件?对于这些类型的问题,我很抱歉,但我现在使用 Access 已经 3 周了,所以我只是一个初学者。
我正在使用名为“import”的导入规范。这可能是问题的原因吗?

下面是目标表的样子:
enter image description here

这是我一直在尝试将数据导入的另一个表。
此表中的字段名称没有特殊字符,字段上设置的数据类型相同,但没有区别。返回相同的错误 31519。

enter image description here

最佳答案

结果证明解决方案很简单:

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

在引号内包含文件名变量,因此它被用作文字字符串(并给出了无效路径)。

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

关于csv - Access VBA - 导入 *.CSV 时出现运行时错误 31519。您无法导入此文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44634081/

相关文章:

excel - 在 Access VBA 中将子例程应用于日期范围

ms-access - 在 Access 2013 中使用 VBA 打开 Word 文档

excel - VBA 将文件复制为不同的文件扩展名

perl - 使用 Perl 解析 CSV 文件从特定行到文件末尾

Java CSV 行分隔

python - Pandas 将 CSV 列中的 '\0' 读取为 NULL 字符并在 JSON 中打印为 Unicode

vb.net - 连接到具有数据库密码的 Access 数据库

vb.net - 通过 VB.NET 合并 excel 单元格,而不询问每个单元格何时有值

excel - 根据单元格内的值过滤列

python - 如何自动命名已抓取日期的每个后续 .CSV 文件?