系统错误,将数据从Excel传输到ACCESS

时间:2019-02-23 17:56:24

标签: excel vba ms-access

我对一些将数据从EXCEL传输到ACCESS的代码有疑问。 这是代码:

 Sub Export_Data()
    Dim cnn As ADODB.Connection 'dim the ADO collection class
    Dim rst As ADODB.Recordset 'dim the ADO recordset class
    Dim dbPath
    Dim x As Long, i As Long
    Dim nextrow As Long

    'add error handling
    'On Error GoTo errHandler:

    'Variables for file path and last row of data
    dbPath = ActiveSheet.Range("T2").Value
    nextrow = Cells(Rows.Count, 1).End(xlUp).Row

    'Initialise the collection class variable
    Set cnn = New ADODB.Connection

    'Check for data
    If Sheet3.Range("B4").Value = "" Then
    MsgBox " Add the data that you want tot send to MS Access"
    Exit Sub
    End If

    'Connection class is equipped with a —method— named Open
    '—-4 aguments—- ConnectionString, UserID, Password, Options
    'ConnectionString formula—-Key1=Value1;Key2=Value2;Key_n=Value_n;
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
    'two primary providers used in ADO SQLOLEDB —-Microsoft.JET.OLEDB.4.0 —-Microsoft.ACE.OLEDB.12.0
    'OLE stands for Object Linking and Embedding, Database

    'ADO library is equipped with a class named Recordset
    Set rst = New ADODB.Recordset 'assign memory to the recordset

    'ConnectionString Open '—-5 aguments—-
    'Source, ActiveConnection, CursorType, LockType, Options
    rst.Open Source:="Transactions", ActiveConnection:=cnn, _
    CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
    Options:=adCmdTable

    'you now have the recordset object
    'add the values to it
    For x = 4 To nextrow
    rst.AddNew
    For i = 2 = 1 To 21
    rst(Cells(1, i).Value) = Cells(x, i).Value
    Next i
    rst.Update
    Next x

    'close the recordset
    rst.Close
    ' Close the connection
    cnn.Close
    'clear memory
    Set rst = Nothing
    Set cnn = Nothing

    'communicate with the user
    MsgBox " The data has been successfully sent to the access database"

    'Update the sheet
    Application.ScreenUpdating = True

    'Clear the data
    Sheet3.Range("A4:G10000").ClearContents
    On Error GoTo 0
    Exit Sub

    'clear memory
    Set rst = Nothing
    Set cnn = Nothing
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Export_Data"
 End Sub

我收到此错误...。

  

SystemError&H80040E4D -2147217843

出了什么问题?我只能说,似乎很难从EXCEL提取数据或将其放入ACCESS

1 个答案:

答案 0 :(得分:0)

我正在使用启用了宏的Excel文件,但遇到了相同的错误

innerHTML

对我来说,执行VBA文件的用户没有访问数据库的权限。

您可以检查用户对服务器(和数据库)的权限。就我而言,这很有帮助。

最佳

j