?
Solved

Access database engine cannot find recordset

Posted on 2014-09-22
3
Medium Priority
?
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 2000 total points
ID: 40336219
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 85
ID: 40336226
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
ID: 40336233
Thanks for quick and accurate reply.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

741 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