Total numbers of records

  • Thread starter Thread starter Harmannus
  • Start date Start date
H

Harmannus

Hallo,

Is there a quick way to count the total numbers of records in a table?

Now i use the below code.

1. Any reasons way this only works on a table that is not linked to a
back-end?
2. Any suggestions for improvements?

Dim db As DAO.Database, rs As DAO.Recordset
Dim Lastrec

' Return reference to current database.
Set db = CurrentDb
' Open table-type Recordset object.
Set rs = db.OpenRecordset("tblOG")
Debug.Print rs.RecordCount
Lastrec = rs.RecordCount
Forms!FrmAbout!totOG = Lastrec
rs.Close

Regards,

Harmannus
 
When you first open a recordset, the RecordCount property indicates the
number of records accessed so far.

For a local table, the default recordset type is dbOpenTable. In this case,
the number of records is available, so Access will typically return the
correct number of records.

For an attached table or a recordset based on a SQL statement, Access opens
a recordset of type dbOpenDynaset, and the count will typically be 0 if
there are no records, or 1 if there are records (i.e. 1 accesses so far). If
you need to know the number of records immediately after opening a
dbOpenDynaset type recordset, use the MoveLast method before reading the
RecordCount.

The easiest code to write to get the count of records in a table would be:
DCount("*", "MyTable")
 
Back
Top