记录集计数返回-1

时间:2017-07-17 18:55:31

标签: excel-vba teradata adodb vba excel

我已经尝试了我可以在网上找到的每一次迭代,但是我仍然在返回-1^我的记录集计数而不是实际计数。我尝试了CursorType, LockType, & CursorLocation的多种组合。这是我的代码。

Sub test()
    Dim FullQry As String
    Dim qry1 As String
    Dim qry2 As String
    Dim qry3 As String
    Dim qry4 As String

'DECLARE VARIABLES FOR CONNECTION (HOW THE QUERY CONNECTS TO TERADATA)
    Dim cn As ADODB.Connection
    Set cn = New ADODB.Connection

'DECLARE VARIABLES FOR RECORDSET (THE RESULTS OF THE SQL QUERY)
    Dim rs As ADODB.Recordset
    Set rs = CreateObject("ADODB.Recordset")

'DECLARE VARIABLES FOR COMMAND (I THINK THIS MAKES TERADATA RUN THE QUERY AFTER A CONNECTION IS ESTABLISHED)
    Dim cmdSQLData As ADODB.Command
    Set cmdSQLData = New ADODB.Command

'Connect to Teradata
    cn.Open "Data Source = MOSAIC_PROD; Database= prod_flight_ops_combined_vw; Persist Security info=True; User ID=758673; Password=PSPL444eae???; Session Mode=System Default;"
    Set cmdSQLData.ActiveConnection = cn
    rs.CursorType = adOpenStatic
    rs.LockType = adLockReadOnly
    rs.CursorLocation = adUseClient

'Define Qry

    qry1 = "SELECT AIRLINE, FLT_NUM,    SKD_ORIG,   SKD_DEST,   ACT_ORIG,   ACT_DEST,   SKD_TAIL,   ACT_TAIL,   SKD_SUBFLEET,   ACT_SUBFLEET,   SKD_OUT_GMT,    SKD_IN_GMT,     ACT_OUT_GMT,        ACT_ON_GMT,     ACT_IN_GMT,    ACT_OUT_DATE_GMT,    ACT_IN_DATE_GMT, ACT_OFF_GMT, "
    qry2 = "SKD_OUT_DATE_GMT , SKD_IN_DATE_GMT, SKD_BLK, ACT_BLK, SKD_AIR, ACT_AIR, SKD_TXOT, ACT_TXOT, SKD_TXIN, ACT_TXIN, SKD_OFF, ACT_OFF, SKD_ON, ACT_ON, SKD_TURN, ACT_TURN, AVAIL_TURN, MOGT, OP_STATUS, OP_STATUS_DESC, SUB_DIVERT_DESC , DELAY_MSG "
    qry3 = "FROM prod_flight_ops_combined_vw.OPS_FLIGHT_LEG "
    qry4 = "WHERE act_out_date_gmt > current_date - 45 and act_in_date_gmt < current_date - 1 and Airline = 'AA';"

FullQry = qry1 & qry2 & qry3 & qry4


    cmdSQLData.CommandText = FullQry
    cmdSQLData.CommandType = adCmdText
    cmdSQLData.CommandTimeout = 0
    Set rs = cmdSQLData.Execute()
    x = rs.RecordCount


Set rs = Nothing
Set cmdSQLData = Nothing
cn.Close
End Sub

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

Sub test()
  Dim cn As ADODB.Connection
  Set cn = New ADODB.Connection
  cn.Open "Data Source=MOSAIC_PROD"

  Dim rs As ADODB.Recordset
  Set rs = CreateObject("ADODB.Recordset")

  rs.CursorLocation = adUseClient

  rs.Open "SELECT * from FROM prod_flight_ops_combined_vw.OPS_FLIGHT_LEG", cn

  MsgBox (rs.RecordCount)


  cn.Close
  Set rs = Nothing
  Set cn = Nothing
End Sub