在 Access 中使用 DoCmd.TransferText 方法时,我收到问题标题中描述的错误。
我的代码正在从当前数据库中的表中获取文件名(这些文件是从单个文件夹中存在的 2000 多个文件中选择的文件),其目标是将这些文件的内容导入到 Access 中的一个表中。
请阅读此主题以获取更多信息:
VBA procedure to import only selected csv files (from one folder) into a single table in access
所有文件都具有相同的结构和数据类型。
这是我正在使用的代码:
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
崩溃.目标表是否应该准备一些特殊的方式来导入这些文件?对于这些类型的问题,我很抱歉,但我现在使用 Access 已经 3 周了,所以我只是一个初学者。
我正在使用名为“import”的导入规范。这可能是问题的原因吗?
下面是目标表的样子:
这是我一直在尝试将数据导入的另一个表。
此表中的字段名称没有特殊字符,字段上设置的数据类型相同,但没有区别。返回相同的错误 31519。
最佳答案
结果证明解决方案很简单:
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/