2014-01-21 36 views
0

我已經搜索了這個答案,但我無法找到任何我可以理解的,因爲我是一個小白點,並尋找外行的解釋。在我的宏中導致自動化錯誤/未指定錯誤2147467259(80004005)是什麼?

當您點擊提交時,該宏將推送一些數據以進行訪問。

只要我可以告訴它可能與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 
+1

哪條線時,拋出的錯誤是突出? – Manhattan

回答

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也不會找到一個重複的行。

細胞對象這樣使用:

細胞(行,列)

HTH 菲利普

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, ... 
相關問題