Search code examples
ms-accessvbams-access-2013

Runtime Error 3011 in Access


I just try to import a .csv file with vba. I use the DoCmd.TransferText method with custom specs. I tried to import the file with the wizard, with the same specs and it works perfectly fine.

But when I start the vba I get this error Message:

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

Here is the part of my code, where I call up the method:

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

My Database is on a Network-Drive, but I tried to copy it on my local drive and it had the same otcome. I also experimented with the File Location.

The Software I am using is: -Microsoft Access 2013

Thank you all in adnvance :)


Solution

  • A more full answer:

    The filename contained non-ASCII characters. Multiple Access functions can't handle this properly. The solution is to rename any file with non-ASCII characters to something that doesn't include these characters.

    Some useful helper functions:

    Test if a string contains non-ASCII characters and return true if it does, false if it doesn't (can be used to throw descriptive errors in this case).

    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
    

    Strip non-ASCII characters from a string

    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