Results 1 to 2 of 2

Thread: How to get the total number of records in a table?

  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 inside an access table?
    I think I should use MoveLast, but I don't remember the correct way to do that...

    Thanks for any help...

    Michel Jr.

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    The following should work:
    Code:
    Dim dbMyDB As Database
    dim lngNbrOfRecs As Long
    
    Set dbMyDB = OpenDatabase("C:\Whatever\MyData.mdb")
    lngNbrOfRecs = dbMyDB.TableDefs("MyTable").RecordCount
    Under DAO, when you access a table as a "table", the RecordCount property reflects the actual number of records in the table. The "MoveLast" thing applies when you open a record as a "dynaset" (which happens for example when you use OpenRecordset with a SQL statement). For example:
    Code:
    Dim dbMyDB As Database
    dim rsMyRs As Recordset
    dim lngNbrOfRecs As Long
    
    Set dbMyDB = OpenDatabase("C:\Whatever\MyData.mdb")
    Set rsMyRs = dbMyDB.OpenRecordset("SELECT blah blah", dbOpenDynaset)
    ' the following will only return 1 regardless of how
    ' many records are returned (or 0 if empty):
    lngNbrOfRecs = rsMyRs.RecordCount
    ' use MoveLast to get the full count:
    rsMyRs.MoveLast
    ' now recordcount will be good:
    lngNbrOfRecs = rsMyRs.RecordCount
    [/code]

    "It's cold gin time again ..."

    Check out my website here.

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