1
我創建了一個從Oracle(64位)到Microsoft Excel 2013(64位)的連接。 VBA按預期方式連接並按預期粘貼列標題,但是一旦我嘗試使用CopyFromRecordSet Excel複製記錄之前凍結。我不知道爲什麼。我沒有收到任何錯誤消息。CopyFromRecordset導致Excel崩潰
這些是我所選擇的參考庫:
- Visual Basic應用程序
- 的Microsoft Excel 15.0對象庫
- OLE自動化
- 的Microsoft Office 15.0對象庫
- Microsoft窗體2.0對象庫
- Microsoft ActiveX數據對象6.1庫
- TabBtnEx 1.0類型庫
- 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
您是否驗證過記錄集中實際存在數據? – Rory
是的,當我添加recSet.recordCount時,我得到2。 –