Solved

Access database engine cannot find recordset

Posted on 2014-09-22
3
288 Views
Last Modified: 2014-09-22
I define a DAO recordset and shows up in "locals" window and when I hover over its fields lower in the code it has values as exoected, but if I want to count the records, I get an error message as below. What is wrong?

I want to count the total number of records in the recordset. (Stops are there for debugging.)

Set rstIncomes = db.OpenRecordset(rstIncomesSQL, dbOpenSnapshot)
Stop

If DCount("*", "rstIncomes") = 0 Then
   MsgBox "rstIncomes has No records to show"
   Stop
   GoTo Proceed
Else
   MsgBox "rstIncomes has " & DCount("*", "rstIncomes") & "records."
   Stop
End If

Open in new window

error messageerror message
0
Comment
Question by:Fritz Paul
3 Comments
 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
Comment Utility
DCount can only be used on a table or query, not on a recordset. For a recordset you should use the RecordCount property, as illustrated in the following code snippet:
Dim rstIncomes As DAO.Recordset

Set rstIncomes = db.OpenRecordset(rstIncomesSQL, dbOpenSnapshot)

With rstIncomes
    .MoveLast
    .MoveFirst
    MsgBox "The rstIncomes has " & .RecordCount & " records!"
End With

rstIncomes.Close
Set rstIncomes = Nothing

Open in new window

0
 
LVL 84
Comment Utility
Or if you simply want a Count of the items:

Dim rst AS DAO.Recordset
Set rst = Currentdb.OpenRecordset("SELECT COUNT(*) AS RecCount FROM YourTable WHERE blah blah blah")

Msgbox "Record count: " & rst("RecCount")
0
 

Author Closing Comment

by:Fritz Paul
Comment Utility
Thanks for quick and accurate reply.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

772 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

10 Experts available now in Live!

Get 1:1 Help Now