Solved

Count the number of rows in a query result set

Posted on 2008-06-16
9
2,726 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
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

860 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