在我的宏中导致自动化错误/未指定错误2147467259(80004005)的原因是什么?

时间:2014-01-21 19:02:59

标签: excel vba excel-vba

我已经找到了答案,但我找不到任何可以理解的东西,因为我有点像菜鸟,正在寻找外行的解释。

当您单击“提交”时,宏将推送一些数据进行访问。

据我所知,它可能与ActiveX数据对象引用有关,但说实话,这是我的第一个这类项目,我真的可以使用一些帮助。

这是我的代码:

Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
    ' connect to the Access database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
        "Data Source=M:\DataBase2.mdb;"
    ' open a recordset
    Set rs = New ADODB.Recordset
    rs.Open "ShiftSwapData", cn, adOpenKeyset, adLockOptimistic, adCmdTable
    ' all records in a table
    r = 3 ' the start row in the worksheet
    Do While Len(Range("A" & r).Formula) > 0
    ' repeat until first empty cell in column A
        With rs
            .AddNew ' create a new record
            ' add values to each field in the record
            .Fields("Date Submitted").Value = Trim(Cells(50, 1).Text)
            .Fields("Agent Email").Value = Trim(Cells(50, 2).Text)
            .Fields("Date Requested").Value = Trim(Cells(50, 3).Text)
            .Fields("Payback Date 1").Value = Trim(Cells(50, 4).Text)
            .Fields("Payback Date 2").Value = Trim(Cells(50, 5).Text)
            .Fields("Shift Start").Value = Trim(Cells(50, 6).Text)
            .Fields("Shift End").Value = Trim(Cells(50, 7).Text)
            .Fields("RDO").Value = Trim(Cells(50, 8).Text)
            .Fields("Call Type").Value = Trim(Cells(50, 9).Text)
            ' add more fields if necessary...
            .Update ' stores the new record
        End With
        r = r + 1 ' next row
    Loop
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

2 个答案:

答案 0 :(得分:0)

您可以尝试更改With...End With块,如下所示:

Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A
    With rs
        .AddNew ' create a new record
        ' add values to each field in the record
        .Fields("Date Submitted").Value = Trim(Cells(r, 1).Text)
        .Fields("Agent Email").Value = Trim(Cells(r, 2).Text)
        .Fields("Date Requested").Value = Trim(Cells(r, 3).Text)
        .Fields("Payback Date 1").Value = Trim(Cells(r, 4).Text)
        .Fields("Payback Date 2").Value = Trim(Cells(r, 5).Text)
        .Fields("Shift Start").Value = Trim(Cells(r, 6).Text)
        .Fields("Shift End").Value = Trim(Cells(r, 7).Text)
        .Fields("RDO").Value = Trim(Cells(r, 8).Text)
        .Fields("Call Type").Value = Trim(Cells(r, 9).Text)
        ' add more fields if necessary...
        .Update ' stores the new record
    End With
loop

现在当它尝试执行.Update时,它将找不到重复的行。

Cells 对象的使用方式如下:

单元格 (行,列)

HTH 菲利普

答案 1 :(得分:0)

Do While Len(Range("A" & r).Formula) > 0 

,对我来说很奇怪,因为对我来说它是一个完整的列,所以要求整个列的公式是奇怪的

可能会将范围部分更改为

len(cells(1,r).formula)>0 

range("A" & trim(str(r))).formula if you mean A3, A4, ...
相关问题