2015-05-12 113 views
0

我正在使用以下代碼從Oracle數據庫中提取一些信息。 我已經多次使用這種類型的Oracle連接和SQL腳本,但是,這個特殊的腳本拋出了應用程序定義或對象定義的錯誤。報告在出現錯誤之前填充7625行。Excel VBA - 應用程序定義或對象定義的錯誤

任何想法?

Sub SQL_CDS() 

Sheets("CDS Data").Select 

Dim SQL As String 
Dim orasession As Object 
Dim oradatabase As Object 
Dim dyprod As Object 
Dim Row As Integer 

Application.ScreenUpdating = False 

Set orasession = CreateObject("OracleInProcServer.XOraSession") 'Create the OraSession Object. (Oracle) 
Set oradatabase = orasession.DbOpenDatabase("sid_cnded", "bo_bca_rep/bobcarep01", 0&) 'Create the OraDatabase Object by opening a connection to Oracle. 

SQL = SQL & "SELECT selection_no as one, " 
SQL = SQL & "selection_status as two, " 
SQL = SQL & "onhand_cds as three, " 
SQL = SQL & "allocated_cds as four, " 
SQL = SQL & "nr_date as five, " 
SQL = SQL & "last_8_weeks_shipped as six, " 
SQL = SQL & "reorder_weeks_shipped as seven, " 
SQL = SQL & "reorder_status_shipped as eight " 
SQL = SQL & "FROM RPAT.BBC_CDS" 

Set dyprod = oradatabase.CreateDynaset(SQL, 0&) 

Sheets("CDS Data").Select 
Row = 2 
     If Not dyprod.EOF And Not dyprod.bof Then 
       dyprod.movefirst 
         Do Until dyprod.EOF 
          Sheets("CDS Data").Cells(Row, 1).Select 
          ActiveCell.Value = dyprod.Fields("one").Value 
          Sheets("CDS Data").Cells(Row, 2).Select 
          ActiveCell.Value = dyprod.Fields("two").Value 
          Sheets("CDS Data").Cells(Row, 3).Select 
          ActiveCell.Value = dyprod.Fields("three").Value 
          Sheets("CDS Data").Cells(Row, 4).Select 
          ActiveCell.Value = dyprod.Fields("four").Value 
          Sheets("CDS Data").Cells(Row, 5).Select 
          ActiveCell.Value = dyprod.Fields("five").Value 
          Sheets("CDS Data").Cells(Row, 6).Select 
          ActiveCell.Value = dyprod.Fields("six").Value 
          Sheets("CDS Data").Cells(Row, 7).Select 
          ActiveCell.Value = dyprod.Fields("seven").Value 
          Sheets("CDS Data").Cells(Row, 8).Select 
          ActiveCell.Value = dyprod.Fields("eight").Value 
          dyprod.movenext 
          Row = Row + 1 
         Loop 
       End If 

    Range("A2").Select 

End Sub 

感謝SMORF

+2

確實錯誤發生哪條線?爲什麼使用ActiveCell?爲什麼不簡單地把價值? – PaulFrancis

+0

錯誤發生在'ActiveCell.Value = dyprod.Fields(「五」)行'...我一直使用ActiveCell ...以前沒有問題? – SMORF

+0

如果你在VBE立即窗格中執行'?ActiveCell.Value',你會得到一個錯誤嗎?怎麼樣??dyprod.Fields(「五」)。值? –

回答

1

沒有選擇(和使用作爲龍行計數):

Sub SQL_CDS() 

Sheets("CDS Data").Select 

Dim SQL As String 
Dim orasession As Object 
Dim oradatabase As Object 
Dim dyprod As Object 
Dim Row As Long '<<<<<< avoid integer values when dealing with rows 

Application.ScreenUpdating = False 

Set orasession = CreateObject("OracleInProcServer.XOraSession") 'Create the OraSession Object. (Oracle) 
Set oradatabase = orasession.DbOpenDatabase("sid_cnded", "bo_bca_rep/bobcarep01", 0&) 'Create the OraDatabase Object by opening a connection to Oracle. 

SQL = SQL & "SELECT selection_no as one, " 
SQL = SQL & "selection_status as two, " 
SQL = SQL & "onhand_cds as three, " 
SQL = SQL & "allocated_cds as four, " 
SQL = SQL & "nr_date as five, " 
SQL = SQL & "last_8_weeks_shipped as six, " 
SQL = SQL & "reorder_weeks_shipped as seven, " 
SQL = SQL & "reorder_status_shipped as eight " 
SQL = SQL & "FROM RPAT.BBC_CDS" 

Set dyprod = oradatabase.CreateDynaset(SQL, 0&) 

Sheets("CDS Data").Select 'not required 

Row = 2 
If Not dyprod.EOF And Not dyprod.bof Then 
    dyprod.movefirst 
    Do Until dyprod.EOF 
      Sheets("CDS Data").Cells(Row, 1).Resize(1,8).Value = _ 
          Array(dyprod.Fields("one").Value, _ 
            dyprod.Fields("two").Value, _ 
            dyprod.Fields("three").Value, _ 
            dyprod.Fields("four").Value, _ 
            dyprod.Fields("five").Value, _ 
            dyprod.Fields("six").Value, _ 
            dyprod.Fields("seven").Value, _ 
            dyprod.Fields("eight").Value) 


     dyprod.movenext 
     Row = Row + 1 
    Loop 
End If 

Range("A2").Select 

End Sub 
+0

非常感謝Tim,我還發現oracle數據庫包含日期格式預計(31/12/0999 =單元格值31/12/999)。這導致了第7625行的錯誤 - 在SQL中修復... replace(to_char(nr_date,'dd/mm/yyyy'),'0999','9999') – SMORF

相關問題