CopyFromRecordset导致Excel崩溃

时间:2016-08-16 14:48:15

标签: excel-vba vba excel

我创建了从Oracle(64位)到Microsoft Excel 2013(64位)的连接。 VBA按预期连接并按预期粘贴列标题,但是一旦我尝试使用CopyFromRecordSet Excel在复制记录之前冻结。我不知道为什么。我没有收到任何错误消息。

这些是我选择的参考库:

  1. Visual Basic for Applications
  2. Microsoft Excel 15.0对象库
  3. OLE自动化
  4. Microsoft Office 15.0对象库
  5. Microsoft Forms 2.0对象库
  6. Microsoft ActiveX数据对象6.1库
  7. TabBtnEx 1.0类型库
  8. Microsoft ActiveX数据对象Recordset 2.8库
  9. 以下是代码:

    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
    

1 个答案:

答案 0 :(得分:0)

我想我明白了。我将CursorTypeEnum从adOpenStatic更改为adOpenForwardOnly,它完美地运行。我怀疑ODBC连接中存在某种类型的错误,不允许adOpenStatic。我正在运行Excel 64位,但是,用于ODBC连接的dll文件在某处有32个。