Re: ADO Recordcount = -1

From: Stephen Howe (stephenPOINThoweATtns-globalPOINTcom)
Date: 01/25/05

  • Next message: DS: "Re: ADO Recordcount = -1"
    Date: Tue, 25 Jan 2005 19:12:25 -0000
    
    

    "DS" <dsweatman@bellsouth.net> wrote in message
    news:1106676303.986530.236150@f14g2000cwb.googlegroups.com...
    > The following code is in a asp page. I can't get the recordcount to
    > return the correct information. The recordset is populated because I
    > can loop through it. How do I get the recordcount to work correctly?
    > Any help would be greatly appreciated!

    RecordCount being -1 is the number 1 question here.
    And if you check the MS documentation, it depends on CursorType
    See
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdprorecordcount.asp
    RecordCount is always -1 if the CursorType is ForwardOnly and can be -1 if
    Dynamic.
    In other cases it is the right number.

    Lets look at your code

    > Dim madoRs,madoCmd,msConn,msSQL
    >
    > Set madoRs = Server.CreateObject("ADODB.Recordset")
    >
    > Set madoCmd = Server.CreateObject("ADODB.Command")
    >
    > msConn = "Provider=SQLOLEDB;Data Source=XXXXXDEV;Initial
    > Catalog=TestDB;UID=xxx;PWD=xxx"
    >
    > msSQL = "select lMSDSId from tblMSDS"
    >
    > With madoRs
    > .MaxRecords = MAX_ROWS
    > .LockType = adLockReadOnly
    > .CursorLocation = adUseClient
    > .CursorType = adOpenStatic
    > End With

    These properties you have setup will be ignored.
    Instead madoRs inherits the properties of the returned RecordSet from
    madoCmd.
    And the returned RecordSet's CursorLocation will be whatever is the default
    CursorLocation inherited from the the Connection used by Command object.
    On the Connection object that is adUseServer by default unless changed. So
    you will _ALWAYS_ get a returned recordset that is

    ServerSided, ForwardOnly, ReadOnly
    or
    ClientSided, Static, ReadOnly

    if you do Cmd.Execute

    If you want to specify the type of RecordSet other than the defaults other
    than above you need to not use .Execute. You have to use a 2nd method.
    In your case, the reason it is likely to be failing is because the
    Connection object's CursorLocation is adUseServer which the Cmd object uses.
    So you are getting a returned Recordset that is ServerSided, ForwardOnly,
    ReadOnly and ForwardOnly cursors always have a RecordCount of -1

    Instead you do

    With madoCmd
       .ActiveConnection = msConn
      .CommandType = adCmdText
      .CommandText = msSQL
      .CommandTimeout = 60
    End With

    With madoRs
     .MaxRecords = MAX_ROWS
     .LockType = adLockReadOnly
     .CursorLocation = adUseClient
     .CursorType = adOpenStatic
    REM .ActiveConnection THIS MUST NOT BE SET, it gets the connection from the
    Cmd object
     .Open madoCmd ' Do RecordSet.Open() with source being the Cmd object
     End With

    > 'empty recordset
    > If err.number = 0 Then
    > If madoRs.RecordCount = 0 Then
    > msMsg = "There are no MSDSs matching your search criteria."
    > End If
    > End If

    Examining RecordCount is not a robust method for determining no records
    (because it can be -1).
    A better method which works for all cursor types (and you now you can use
    Server-sided ForwardOnly cursors, the fastest cursor, as you are no longer
    dependent on RecordCount being populated) is

    ' There can't be any records if BOF and EOF are both true
    If madoRs.BOF and madoRs.EOF Then
     msMsg = "There are no MSDSs matching your search criteria."
    End If

    Cheers

    Stephen Howe


  • Next message: DS: "Re: ADO Recordcount = -1"

    Relevant Pages

    • Re: how to count a recordset
      ... you can force the RecordCount to be correct (and ... > CursorLocation of adUseClient the RecordCount was accurate. ... > The difference is when the CursorLocation is set to adUseServer. ...
      (microsoft.public.vb.database.ado)
    • Re: how to count a recordset
      ... > I guess if it were up to me, then I'd rename RecordCount to RecordIndex ... with a CursorLocation of adUseClient the RecordCount was accurate. ... The difference is when the CursorLocation is set to adUseServer. ...
      (microsoft.public.vb.database.ado)
    • Re: add data from a datagrid
      ... what can I do, I use on a form a datagrid, and a commandbutton. ... .CursorLocation = adUseServer ... Set dgImport.DataSource = rstincarca ... Otherwise the RecordCount property may not be correct. ...
      (microsoft.public.vb.general.discussion)
    • OleDB Provider RecordCount return -1
      ... I had develop an oledb provider that works fine except when I ask with ADO ... The problem occur when my cursorlocation is ... aduseserver the recordcount method returns -1. ...
      (microsoft.public.data.oledb)
    • Re: Please help with cmd.execute select SQL?
      ... > What is the proper format for my SQL str using command.execute? ... RecordCount requires the use of an expensive cursor. ... and connection immediately, releasing the connection back to the connection ...
      (microsoft.public.inetserver.asp.general)