我只是尝试使用 vba 导入一个.csv文件。 我将DoCmd.TransferText方法与自定义规范一起使用。我尝试使用向导导入文件,具有相同的规格,并且工作正常。
但是当我启动vba时,我收到此错误消息:
对不起,它是德语的,但我认为可以阅读要点
这是我的代码部分,我在其中调用该方法:
Public Function ImportFileToTable(params As ImportFileToTableParams) As ImportFileToTableResult
'TODO: Import a CSV File into the selected table
Dim result As New ImportFileToTableResult
On Error GoTo ImportFail
DoCmd.TransferText acImportDelim, params.SpecificationName, params.TableName, params.FileName, params.HasFieldNames
result.Success = True
Set ImportFileToTable = result
Exit Function
ImportFail:
result.Success = False
result.ErrorMessage = "There was an error importing the File"
Set ImportFileToTable = result
End Function
我的数据库在网络驱动器上,但我尝试将其复制到本地驱动器上,并且它具有相同的功能。我还尝试了文件位置。
我使用的软件是: -Microsoft访问2013
谢谢大家在:)
更完整的答案:
文件名包含非 ASCII 字符。多个访问函数无法正确处理此问题。解决方案是将任何包含非 ASCII 字符的文件重命名为不包含这些字符的文件。
一些有用的帮助程序函数:
测试字符串是否包含非 ASCII 字符,如果包含,则返回 true,如果不包含,则返回 false(在这种情况下可用于引发描述性错误)。
Public Function StringContainsNonASCII(str As String) As Boolean
Dim i As Integer
'Default is false
StringContainsNonASCII = False
'Remove question marks
str = Replace(str, "?", "")
For i = 1 To Len(str)
'Search for question marks
If Asc(Mid(str, i, 1)) = 63 Then
StringContainsNonASCII = True
Exit Function
End If
Next i
End Function
从字符串中删除非 ASCII 字符
Public Function RemoveNonASCII(str As String) As String
Dim i As Integer
For i = 1 To Len(str)
'Append the question marks
If Mid(str, i, 1) = "?" Then
RemoveNonASCII = RemoveNonASCII & "?"
End If
'Append anything that isn't a questionmark
If Asc(Mid(str, i, 1)) <> 63 Then
RemoveNonASCII = RemoveNonASCII & Chr(Asc(Mid(str, i, 1)))
End If
Next i
End Function