Access中的运行时错误3011

时间:2017-07-05 09:30:29

标签: vba access-vba ms-access-2013

我只是尝试使用vba导入.csv文件。 我使用自定义规范的DoCmd.TransferText方法。我尝试使用向导导入文件,使用相同的规格,它完全正常。

但是当我启动vba时,我收到此错误消息:

Sorry, it is in german, but I think one can read the essentials

这是我的代码的一部分,我调用了这个方法:

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 Access 2013

谢谢大家:)

1 个答案:

答案 0 :(得分:1)

更完整的答案:

文件名包含非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