Results 1 to 9 of 9

Thread: [RESOLVED] why recordcount returns -1?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    48

    Resolved [RESOLVED] why recordcount returns -1?

    Why I get recordcount=-1 in the following program?
    Code:
    Call connectionOpen
        Dim rs As ADODB.Recordset
        Set rs = New ADODB.Recordset
        Dim strSQL As String
        strSQL = "SELECT * FROM daily_list_cell WHERE show='Yes'"
        rs.Open strSQL,connection
        MsgBox rs.RecordCount
        rs.Close
        Set rs = Nothing
        Call connectionClose
    Last edited by abdbuet; Aug 27th, 2007 at 02:26 AM.

  2. #2
    Addicted Member
    Join Date
    Aug 2007
    Location
    India
    Posts
    141

    Re: why recordcount returns -1?

    I guess following things need to done
    1) Attach a connection to the recordset.
    rs.Open strSQL, CONNECTION_NAME

    2) You need to set the cursorType and LockType for recordset.

    3) Check your sql query before running to through ADO

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    48

    Re: why recordcount returns -1?

    I am sorry for mistake in the post. I did add Connection_name in rs.open (now i corrected that in previous post)

    Now my question is it really necessary to add cursorType and LockType to get recordcount?

    And I run the sql query in Access and it successfully show 16 records. So nothing wrong in sql.

  4. #4
    Addicted Member
    Join Date
    Aug 2007
    Location
    India
    Posts
    141

    Re: [RESOLVED] why recordcount returns -1?

    You need to set the rs.CursorLocation = adUseClient property.

    It's not necessarry to set the CursorType and LockType, but you need to set the CursorLocation property to get the recordcount.


  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [RESOLVED] why recordcount returns -1?

    Read this:

    Use the RecordCount property to find out how many records are in a Recordset object. The property returns -1 when ADO cannot determine the number of records or if the provider or cursor type does not support RecordCount. Reading the RecordCount property on a closed Recordset causes an error.

    If the Recordset object supports approximate positioning or bookmarks — that is, Supports (adApproxPosition) or Supports (adBookmark), respectively, return True — this value will be the exact number of records in the Recordset, regardless of whether it has been fully populated. If the Recordset object does not support approximate positioning, this property may be a significant drain on resources because all records will have to be retrieved and counted to return an accurate RecordCount value.

    The cursor type of the Recordset object affects whether the number of records can be determined. The RecordCount property will return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and either -1 or the actual count for a dynamic cursor, depending on the data source.


    Code:
    'Try 
       rs.MoveLast
    'before access Recordcount 
       Msgbox rs.RecordCount
    'problem usually happens with queries, not likely with tables
    Last edited by anhn; Aug 27th, 2007 at 03:58 AM.

  6. #6
    Addicted Member
    Join Date
    Aug 2007
    Location
    India
    Posts
    141

    Re: [RESOLVED] why recordcount returns -1?

    Thanks.

    Now, you understand why did i said in my first post to set the CursorType, LockType property.

    In my second post assumed you have set up the CursorType and LockType property correctly,

    Reason for the not returning recordcount could be any wrong property setting.

    Any way you solved your problem...

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    48

    Re: [RESOLVED] why recordcount returns -1?

    thanx guys .. This is why I love this forum becoz I got detail answer . Btw, In a previous project, I did not use cursor location in any form (and I didn't require recordcount property). That software is used by many people and the Database (Access) resides on a server. Does this create any problem in future for not using cursor location?

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] why recordcount returns -1?

    That's not a problem at all - in fact it is better to leave the CursorLocation as the default (Server), as it is faster, uses less memory (well actually for Access it doesn't, but it does for most DBMS's!), and also gives fewer problems in terms of locking etc when the database is used by multiple people at the same time.

    You will probably find useful info in the FAQ article What do the parameters of the recordset.Open method mean? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] why recordcount returns -1?

    Another way to get the count is with an aggregate query. That method may or may not suitable to your needs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width