没有数据传输到具有ADODB记录集的Access

时间:2018-10-11 14:10:13

标签: excel vba ms-access

我用VBA创建了一个excel链接访问数据库,该数据库在使用集中保存的版本时有效,但是在保存本地副本时无效。

我已经使用了调试工具,并且代码跳过了本地保存副本中的For循环。

For x = 2 To nextrow
        DatabaseData.AddNew
        For i = 1 To 35
            DatabaseData(Cells(1, i).Value) = Sheet18.Cells(x, i).Value
            Next i
            DatabaseData.Update
        Next x

我认为这是因为未识别Recordset(DatabaseData)(不确定这是否是正确的术语)。

代码在下面

Sub CopyDatatoAccess()
    Dim DatabaseConn As ADODB.Connection
    Dim DatabaseData As ADODB.Recordset
    Dim Pathway
    Dim x As Long, i As Long
    Dim nextrow As Long

    On Error GoTo errorhandler:

    Pathway = Sheet18.Range("AQ2").Value
    nextrow = Sheet18.Range("AR2")


    Set DatabaseConn = New ADODB.Connection

    If Sheet18.Range("A2").Value = "" Then
    MsgBox "ARF form is not present for Upload"
    Exit Sub
    End If

    DatabaseConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Pathway

    Set DatabaseData = New ADODB.Recordset

DatabaseData.Open Source:="ARFs", _
    ActiveConnection:=DatabaseConn, _
    CursorType:=adOpenDynamic, _
    LockType:=adLockOptimistic, _
    Options:=adCmdTable

For x = 2 To nextrow
    DatabaseData.AddNew
    For i = 1 To 35
        DatabaseData(Cells(1, i).Value) = Sheet18.Cells(x, i).Value
        Next i
        DatabaseData.Update
    Next x

    DatabaseData.Close
    DatabaseConn.Close

    Set DatabaseData = Nothing
    Set DatabaseConn = Nothing

MsgBox "The ARF is now uploaded"
Application.ScreenUpdating = True

Sheet18.Cells.Range("AK2").Value = Sheet18.Cells.Range("AK4").Value + 1

   On Error GoTo 0
   Exit Sub
errorhandler:

    Set DatabaseData = Nothing
    Set DatabaseConn = Nothing
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Export_Data"
    End Sub

0 个答案:

没有答案