Solved

Count the number of rows in a query result set

Posted on 2008-06-16
9
2,738 Views
Last Modified: 2013-11-27
I have a procedure that, depending on whether the query returns 0 rows it must do something and if it returns 1 or more, do something else.  So, how so I simply count the number of rows in a query?  I need to count the number or rows in qryPauseDurations first, if 0, then I only want to run the one query in the ElseIf portion.
Private Sub RunDurations()
'Runs queries to calculate durations
If RowCount >= 1 Then
 
        DoCmd.OpenQuery "qryPauseDurations", acViewNormal 'Need to    count number of rows in this query
 
        DoCmd.OpenQuery "qryStepDurations", acViewNormal
        DoCmd.OpenQuery "qryTaskDurations", acViewNormal
        DoCmd.OpenQuery "qryUpdatePauseStepDurations", acViewNormal
        DoCmd.Close acQuery, "qryPauseDurations"
        DoCmd.Close acQuery, "qryStepDurations"
        DoCmd.Close acQuery, "qryTaskDurations"
        DoCmd.Close acQuery, "qryUpdatePauseStepDurations"
ElseIf RowCount = 0 Then
        DoCmd.OpenQuery "qryUpdateStepOnly", acViewNormal
End If
End Sub

Open in new window

0
Comment
Question by:ssmith94015
[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
  • 6
  • 3
9 Comments
 
LVL 75
ID: 21797183
If DCount("*", "qryPauseDurations") = 0  then

0
 
LVL 75
ID: 21797196
Private Sub RunDurations()
'Runs queries to calculate durations
If DCount("*", "qryPauseDurations") >= 0  then
 
        DoCmd.OpenQuery "qryPauseDurations", acViewNormal 'Need to    count number of rows in this query
 
        DoCmd.OpenQuery "qryStepDurations", acViewNormal
        DoCmd.OpenQuery "qryTaskDurations", acViewNormal
        DoCmd.OpenQuery "qryUpdatePauseStepDurations", acViewNormal
        DoCmd.Close acQuery, "qryPauseDurations"
        DoCmd.Close acQuery, "qryStepDurations"
        DoCmd.Close acQuery, "qryTaskDurations"
        DoCmd.Close acQuery, "qryUpdatePauseStepDurations"
Else
        DoCmd.OpenQuery "qryUpdateStepOnly", acViewNormal
End If
End Sub
0
 
LVL 75
ID: 21797211
And you won't need this:

        DoCmd.OpenQuery "qryPauseDurations", acViewNormal 'Need to    count number of rows in this query

unless you really want to open also ??

Private Sub RunDurations()
'Runs queries to calculate durations
If DCount("*", "qryPauseDurations") >= 0  then
         DoCmd.OpenQuery "qryStepDurations", acViewNormal
        DoCmd.OpenQuery "qryTaskDurations", acViewNormal
        DoCmd.OpenQuery "qryUpdatePauseStepDurations", acViewNormal
        DoCmd.Close acQuery, "qryPauseDurations"
        DoCmd.Close acQuery, "qryStepDurations"
        DoCmd.Close acQuery, "qryTaskDurations"
        DoCmd.Close acQuery, "qryUpdatePauseStepDurations"
Else
        DoCmd.OpenQuery "qryUpdateStepOnly", acViewNormal
End If
End Sub
0
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.

 

Author Comment

by:ssmith94015
ID: 21797335
No, don't care to open, be right back.
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 21797359
***I made a TYPO ***

Private Sub RunDurations()
'Runs queries to calculate durations
If DCount("*", "qryPauseDurations") > 0  then                                     '   ** HAD TYPO HERE >=0
         DoCmd.OpenQuery "qryStepDurations", acViewNormal
        DoCmd.OpenQuery "qryTaskDurations", acViewNormal
        DoCmd.OpenQuery "qryUpdatePauseStepDurations", acViewNormal
        DoCmd.Close acQuery, "qryPauseDurations"
        DoCmd.Close acQuery, "qryStepDurations"
        DoCmd.Close acQuery, "qryTaskDurations"
        DoCmd.Close acQuery, "qryUpdatePauseStepDurations"
Else
        DoCmd.OpenQuery "qryUpdateStepOnly", acViewNormal
End If
End Sub
0
 

Author Comment

by:ssmith94015
ID: 21797406
Ok, it recorgnized if it i 1 or more and runs the first if code.  However, it simply does not recognize what to do if there are 0 rows.  Let me check the query for that first.....
0
 
LVL 75
ID: 21797434
I had a typo - see my correction above  @ http:#a21797359
0
 

Author Comment

by:ssmith94015
ID: 21797545
Thanks DatabaseMX, it seems to be working.
0
 
LVL 75
ID: 21797562
you are welcome.  sorry for the typo confusion ...

mx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

630 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