Results 1 to 8 of 8

Thread: Easy - Check why my RecordCount is not working

  1. #1

    Thread Starter
    Addicted Member Michel Jr's Avatar
    Join Date
    Jan 2000
    Location
    Brazil
    Posts
    175

    Question

    Hi,

    How can I get the number of records in a recordset?
    I'm using ADO.

    This is my code:

    Rst.Open "Select PREmp.*, PRFil.EmpCod, PRFil.FilCod, PRFil.FilIde FROM PREmp INNER JOIN PRFil ON PREmp.EmpCod = PRFil.EmpCod", Connec

    If Rst.RecordCount = 0 Then
    vString = "No records available"
    Combo1.AddItem vString
    End If

    Combo1.Text = Combo1.List(0) ' Show the first item

    Note: the query above also can return an empty result.

    When no records are returned, the message "No records avalilabe" should be inserted in the Combo, but it's not.
    Is the RecordCount property ok? What's wrong in my code above?

    Thanks,

    Michel Jr.

  2. #2
    Lively Member
    Join Date
    Feb 2001
    Location
    Malaysia
    Posts
    71
    The problem is you did not set the cursor type of the recordset.
    There are 4 cursor types: adOpenDynamic, adOpenForwardOnly, adOpenKeyset, and adOpenStatic. The default is adOpenForwardOnly.
    For dynamic and forward only cursors, the RecordCount will always be -1.

  3. #3
    Member
    Join Date
    Mar 2001
    Location
    Hyderabad,India
    Posts
    37

    u got it

    use < 1 instead of =0
    A.Srikanth
    Application Engineer

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    If mRS.BOF Then
      vString = "No records available" 
      Combo1.AddItem vString 
    End If
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by HeSaidJoe
    Code:
    If mRS.BOF Then
      vString = "No records available" 
      Combo1.AddItem vString 
    End If
    Perhaps checking both .BOF and .EOF ?

    Code:
    If mRS.BOF AND mRS.EOF Then
       MsgBox "No Records Found"
    End If
    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  6. #6
    New Member
    Join Date
    Jan 2001
    Posts
    3
    You realy need to do a rec.MoveLast here

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    yap, I agree with Desmond, whereby you need to move the recordset to the last position and then move it back to the first record location. This will ensure you've a correct recordcount.

  8. #8
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Agreed that by moving to the end and then returning to the beginning of the recordset, you get an accurate number of records in the recordset. But if you have a blank recordset, .MoveLast will fail. So you need a method to ascertain that there is at least one record in the recordset so you can safely use .MoveLast and that's when you can check for the .BOF and .EOF properties. When you make sure that the recordset is not empty, you can safely use the .MoveLast and .MoveFirst methods.

    The following code would fail:

    Code:
    'There are no records in mRS
    '
    mRS.MoveLast
    mRS.MoveFirst
    MsgBox "Records : " & CStr(mRS.RecordCount)
    But if you write it like this, it would always work:
    Code:
    If mRS.BOF And mRS.EOF Then
       MsgBox "There are no records"
    Else
       mRS.MoveLast
       mRS.MoveFirst
       MsgBox "Records : " & CStr(mRS.RecordCount)
    End If
    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

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