简体   繁体   中英

Issues executing a ADO Macro from new sheet

I'm trying to execute a macro from a new worksheet with a button so that it runs in another worksheet (named "ARF Export").

Unfortunately I don't know how to set the worksheet I want the macro to run in to ("ARF Export"). Please could you advise me on how to proceed?

The error I get when I run this code in a different sheet is:

Error 3265 Item cannot be found in the collection corresponding to the requested name or ordinal in procedure export_data

When I step into Debug I don't get an error until the end but it skips through my For Loop on line 38 next i

for x = 2 To nextrow
        DatabaseData.AddNew
        For i = 1 To 35
            DatabaseData(Cells(1, i).Value) = Worksheets("ARF Export").Cells(x, i).Value
            Next i
        DatabaseData.Update
    Next x

All code below---

Option Explicit

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 = Worksheets("ARF Export").Range("AR2").Value
    nextrow = Worksheets("ARF Export").Range("As2").Value


    Set DatabaseConn = New ADODB.Connection

    If Worksheets("ARF Export").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) = Worksheets("ARF Export").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

Worksheets("ARF Export").Cells.Range("AK2").Value = Worksheets("ARF Export").Cells.Range("AK4").Value

Worksheets("ARF Export").Cells.Range("AK5").Value = Worksheets("ARF Export").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

Thanks for the help

-Turns out I needed to reference DatabaseData(Cells(1, i).Value) once i did this

For x = 2 To nextrow
    DatabaseData.AddNew
    For i = 1 To 35
        DatabaseData(Worksheets("ARF Export").Cells(1, i).Value) = Worksheets("ARF Export").Cells(x, i).Value
        Next i
        DatabaseData.Update
    Next x

It worked great. Thank you for your help all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM