【问题标题】:Recordset not opening when exporting data from Excel to Teradata将数据从 Excel 导出到 Teradata 时记录集未打开
【发布时间】:2018-07-06 04:22:07
【问题描述】:

我正在尝试使用 Teradata 将表格从 Excel 导出到数据库中。我知道我有与数据库的连接,但记录集未打开,我收到错误 3704“关闭对象时不允许操作。这是我的代码。

    Dim FullQry As String
    Dim qry1 As String
    Dim qry2 As String
    Dim qry3 As String
    Dim qry4 As String
    Dim wb As Workbook, nWB As Workbook
    Dim oWS As Worksheet, oExWS As Worksheet
    Dim y As Long, z As Long
    Dim aRange As Range, bRange As Range
    Dim aData() As Variant

    Application.StatusBar = "Pulling actuals data from TeraData"
    DoEvents

    Set wb = ThisWorkbook
    Set oWS = wb.Sheets("LastRanSchedule")
' Set data range/array
    With oWS
        y = .Cells(2, 1).End(xlDown).Row
        z = .Cells(1, 1).End(xlToRight).Column
        Set aRange = .Range(.Cells(2, 1), .Cells(y, z))
        aData = aRange
    End With

'DECLARE VARIABLES FOR CONNECTION
    Dim cn As ADODB.Connection
    Set cn = New ADODB.Connection
    cn.ConnectionTimeout = 120
    cn.CommandTimeout = 120

'DECLARE VARIABLES FOR RECORDSET
    Dim RS As ADODB.Recordset
    Set RS = New Recordset

'DECLARE VARIABLES FOR COMMAND 
    Dim cmdSQLData As ADODB.Command
    Set cmdSQLData = New ADODB.Command

'Connect to Teradata
    On Error GoTo errhndlr
    cn.Open "Data Source = EDTDPAP1; Database= PROD_TECHOPS_LMP_SNBX_DB.AAL_Tableau_TestData; Persist Security info=True; User ID=" & UserID & "; Password=" & UserPassword & "; Session Mode=System Default;"
    On Error GoTo 0
    Set cmdSQLData.ActiveConnection = cn
    RS.CursorType = adOpenKeyset
    'RS.LockType = adLockOptimistic
    RS.CursorLocation = adUseClient

'Export Data
Dim val As Variant
Debug.Print cn.State
Debug.Print RS.State
For i = 1 To y - 1
   RS.AddNew
    For j = 1 To z
        val = aData(i, j)
        If IsEmpty(val) Then
        Else
            RS.Fields(j) = val
        End If
    Next j
Next i
RS.UpdateBatch

单步执行代码,RS.AddNew 上弹出错误。我的 debug.print 代码确认我的连接已打开,但记录集已关闭。我已经没有想法了,真的可以使用一些建议。谢谢。

【问题讨论】:

    标签: excel odbc teradata vba


    【解决方案1】:

    所以我打开了记录集,虽然这会产生另一个问题,但如果需要,我会发布一个新问题。这是更新的代码。变化在于 RS 连接。

    'Connect to Teradata
        On Error GoTo errhndlr
        cn.Open "Data Source = EDTDPAP1; Database= PROD_TECHOPS_LMP_SNBX_DB.AAL_Tableau_TestData; Persist Security info=True; User ID=" & UserID & "; Password=" & UserPassword & "; Session Mode=System Default;"
        On Error GoTo 0
    
    'DECLARE VARIABLES FOR RECORDSET (THE RESULTS OF THE SQL QUERY)
        Dim RS As ADODB.Recordset
        Set RS = New Recordset
        RS.CursorLocation = adUseClient
        RS.CursorType = adOpenKeyset
        RS.LockType = adLockOptimistic
        RS.Open "SELECT * FROM PROD_TECHOPS_LMP_SNBX_DB.AAL_Tableau_TestData", cn
        Set cmdSQLData.ActiveConnection = cn
    

    【讨论】:

      【解决方案2】:

      我是这样用的。

      Dim Rs As ADODB.Recordset
      Dim strConn As String
      
      
      
      strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
          "Data Source=" & ThisWorkbook.Path & dbName
      
      Set Rs = New ADODB.Recordset
      
      
      With Rs
          .ActiveConnection = strConn
          .CursorType = adOpenDynamic
          .LockType = adLockOptimistic
          .Source = "SELECT * FROM table "
          .ActiveConnection = strConn
          .CursorType = adOpenDynamic
          .LockType = adLockOptimistic
          .Open
          For i = 1 To y - 1
              For j = 1 To Z
                  Val = aData(i, j)
                  If IsEmpty(Val) Then
                  Else
                      .AddNew
                      .Fields(j) = Val
                      .Update
      
                  End If
              Next j
          Next i
          .Close
      End With
      Set Rs = Nothing
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-20
        • 1970-01-01
        • 1970-01-01
        • 2016-07-04
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多