Solved

Access - Not able to get record count

Posted on 2016-09-11
4
34 Views
Last Modified: 2016-10-02
I am trying to open a table and count the records.  I keep getting record count is one.  However when i past the code of my select statement into a query i get the number which i am looking for which is 13.
I eventually would like to filter the table to reduce the results.



  1. Dim db As DAO.Database
  2. Dim rs, rs2 As DAO.Recordset
  3. Dim strSQL, RIGACCT, ClientID, dispname, comp, acctno, bal, exp, expdel, EXPDELDATE, eq, eqdel, EQDELDATE, tu, tudel, tuddate As String
  4. Dim pudate, credotes, newpudate As String
  5. strSQL = "SELECT CREDITREPORT.RIGACCT_FK, CREDITREPORT.CLIENTID_FK, CREDITREPORT.DISPLAYNAME, CREDITREPORT.COMPANYNAME, CREDITREPORT.ACCOUNTNUMBER, CREDITREPORT.BALANCE, CREDITREPORT.EXPERIAN," _
  6.        & " CREDITREPORT.EXPERIANDEL, CREDITREPORT.EXPDELDATE, CREDITREPORT.EQUIFAX, CREDITREPORT.EQUIFAXDEL, CREDITREPORT.EQDELDATE, CREDITREPORT.TRANSUNION, CREDITREPORT.TRANSUNIONDEL, " _
  7.        & "CREDITREPORT.TUDELDATE, CREDITREPORT.PULLEDDATE, CREDITREPORT.CREDREPORTNOTES, CREDITREPORT.NEWPULLEDDATE " _
  8.        & "FROM CREDITREPORT;"
  9. Set db = CurrentDb
  10. Set rs = db.OpenRecordset(strSQL)
  11. With rs
  12.    Debug.Print .RecordCount
  13.    Debug.Print strSQL
  14.    Debug.Print db.OpenRecordset(strSQL).RecordCount
  15. End With
  16. Set rs = Nothing
  17. Set db = Nothing
0
Comment
Question by:VGuerra67
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 250 total points (awarded by participants)
ID: 41793450
You can do:
With rs
   .MoveLast
   .MoveFirst
   Debug.Print .RecordCount
   Debug.Print strSQL
   Debug.Print db.OpenRecordset(strSQL).RecordCount
End With

Open in new window

/gustav
1
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 125 total points (awarded by participants)
ID: 41793555
You can also use DCOUNT:

DCOUNT("RIGACCT_FK", "CREDITREPORT")
0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 125 total points (awarded by participants)
ID: 41796439
The reason to use the DCount() suggestion over the DAO/Movelast is because with the DAO method, you are forcing Access to physically retrieve each row and bring it from the server to your local PC in order to count the fully populated LOCAL recordset.  With the DCount() method, the answer can be determined by the query engine simply looking at a table statistic and not retrieving any data at all.  The larger the row count, the more important the distinction.

Or, if you are welded to the DAO methodology, at least use a totals query:

Select Count(*) as RecCount from CREDITREPORT;

Then when you open the recordset, it contains a single row and that row contains your count.

The DCount() is essentially the same as the query above.  Both return a single value and both will be optimized to use table stats rather than being forced to retrieve individual rows.
0
 
LVL 84
ID: 41825212
Any of the comment would provide the user with the record count
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now