2015-10-02 74 views
1

下面的代碼正確導出數據,但是我的debug.print行總是給出-1。我想檢索debug.print聲明中的行數。請提出什麼是錯的。ADODB RecordSet.RecordCount屬性始終返回-1

Sub TEST() 

     Dim rs As Object 
     Dim iCols As Integer 
     Set rs = CreateObject("ADODB.Recordset") 
     On Error GoTo ERR 
     Dim SQLSTR As String, MYVAL As String 
     MYVAL = InputBox("Enter Query") 
     SQLSTR = " " & MYVAL & "" 
     CONNECT_TO_DWHS 
     rs.Open SQLSTR, PERSONALDBCONT 

      For iCols = 0 To rs.Fields.Count - 1 
       ActiveSheet.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name 
      Next 
     Debug.Print rs.RecordCount 
     ActiveSheet.Cells(2, 1).CopyFromRecordset rs 
     ActiveSheet.Cells(1, 1).Select 

     CLOSE_CONNECTION_TO_SQL 

      With ActiveWindow 
       .SplitColumn = 0 
       .SplitRow = 1 
       .FreezePanes = True 
      End With 

     Exit Sub 

    ERR: 
     CLOSE_CONNECTION_TO_SQL 
     MsgBox "There was an error" 

    End Sub 
+0

我認爲這是因爲在FOR操作中發生的遞減。爲了使循環停止,最後執行的操作是減少1個'rs.Fields.Count'值,爲了循環結束,該值爲'-1'。因此,*我認爲*'print'會一直顯示-1。 (*但這只是一個想法,它完全不是程序員的迴應類型,應該是精確*)。爲了檢查這一點,我建議你在循環內移動'print',或者甚至可以在循環之前打印記錄數。 –

回答

0

當你打開一個RecordSetForwardOnly(默認值,如果沒有明確的聲明遊標類型)RecordCount屬性返回-1。

更多檢查here