在 Access 中使用 DoCmd.TransferText 方法时,我收到问题标题中描述的错误。我的代码从当前数据库的表中获取文件名(这些文件是从单个文件夹中存在的 2000 个以上文件中选择的),其目标是将这些文件的内容导入到 Access 中的一个表中。请阅读本主题以获取更多信息: VBA procedure to import only selected csv files (from onefolder) 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
Run Code Online (Sandbox Code Playgroud)
代码在 处崩溃DoCmd.TransferText acImportDelim, "import", "all_stocks_1", "my_path & rs.Fields(0).Value", True。
目标表是否应该准备一些特殊的方式来导入这些文件?很抱歉提出此类问题,但我已经使用 Access 3 周了,所以我只是一个初学者。我正在使用称为“导入”的导入规范。这可能是问题的原因吗?
这是我一直试图将数据导入到的另一个表。该表中的字段名称没有特殊字符,字段上设置的数据类型相同,但没有区别。返回相同的错误 31519。
解决方案很简单:
DoCmd.TransferText acImportDelim, "import", "all_stocks_1", _
"my_path & rs.Fields(0).Value", True
Run Code Online (Sandbox Code Playgroud)
文件名变量包含在引号内,因此它被用作文字字符串(并给出了无效路径)。
正确的:
DoCmd.TransferText acImportDelim, "import", "all_stocks_1", _
my_path & rs.Fields(0).Value, True
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4478 次 |
| 最近记录: |