2016-08-16 89 views
1

我創建了一個從Oracle(64位)到Microsoft Excel 2013(64位)的連接。 VBA按預期方式連接並按預期粘貼列標題,但是一旦我嘗試使用CopyFromRecordSet Excel複製記錄之前凍結。我不知道爲什麼。我沒有收到任何錯誤消息。CopyFromRecordset導致Excel崩潰

這些是我所選擇的參考庫:

  1. Visual Basic應用程序
  2. 的Microsoft Excel 15.0對象庫
  3. OLE自動化
  4. 的Microsoft Office 15.0對象庫
  5. Microsoft窗體2.0對象庫
  6. Microsoft ActiveX數據對象6.1庫
  7. TabBtnEx 1.0類型庫
  8. Microsoft ActiveX數據對象Recordset 2.8庫

下面是代碼:

Sub getdata3() 
Dim UserName As String 
Dim PassWord As String 
Dim con As Object 
Dim recSet As Recordset 
Dim SQL_String As String 
Dim dbConnectStr As String 
Dim recordCount As Long 
Dim col As Integer 

'Set up variables 
UserName = "unid" 
PassWord = "pwd" 
dbConnectStr = "DSN=DW;Uid=" & UserName & ";Pwd=" & PassWord 


'Set connection parameters 
Set con = New ADODB.Connection 
Set recSet = New ADODB.Recordset 
con.ConnectionString = dbConnectStr 
con.ConnectionTimeout = 30 
con.Open dbConnectStr 

'Query 
SQL_String = "SELECT * FROM U0408224.D_FLAG WHERE DWID = 675863" 
recSet.Open SQL_String, con, adOpenStatic 

'Recordset to sheet 
col = 0 
Do While col < recSet.Fields.Count 
Sheets("SQL").Cells(20, col + 1) = recSet.Fields(col).Name 
col = col + 1 
Loop 
Sheets("SQL").Cells(21, 1).CopyFromRecordset recSet 

'Close recordsets and connections 
recSet.Close 
con.Close 
Set con = Nothing 
Set recSet = Nothing 


End Sub 
+0

您是否驗證過記錄集中實際存在數據? – Rory

+0

是的,當我添加recSet.recordCount時,我得到2。 –

回答

0

我想我想通了。我將CursorTypeEnum從adOpenStatic更改爲adOpenForwardOnly,並且完美運行。我懷疑在ODBC連接中有一些不允許adOpenStatic的錯誤。我運行Excel 64位,但是,用於ODBC連接的dll文件在其中有32個文件。