?
Solved

Count the number of rows in a query result set

Posted on 2008-06-16
9
Medium Priority
?
2,741 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

719 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