简体   繁体   中英

Recordcount returns value of -1

I have the below code which retrieves data correctly however always return a value of -1 for rs.RecordCount. Please assist.

    Sub TEST()

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

        Dim SQLSTR As String, MYVAL As String
        MYVAL = InputBox("Enter Query")
        SQLSTR = " " & MYVAL & ""
        CONNECT_TO_DWHS
        rs.Open SQLSTR, PERSONALDBCONT

        ActiveSheet.Cells(2, 1).CopyFromRecordset rs
        ActiveSheet.Cells(1, 1).Select

            Debug.Print rs.RecordCount

        CLOSE_CONNECTION_TO_SQL


    End Sub

The ADO Recordsets will ever show this, until they've jumped to the end the first time. So just use rs.Movelast, then rs.MoveFirst.

Now it should be OK...

Hope this helps

rs.CursorType = 3似乎可以工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM