Count at Group Level

P

PB

I have a report, grouped by EmployeeId, I need a count of the number of
employees included in the report. In the EmployeeID group header, I have a
textbox control, control source =1, runing sum = over group. Sometimes the
total is correct, sometimes it is off by 1. Any ideas why this isn't working?
 
K

Ken Sheridan

Setting the RunningSum property to 'over group' is intended to give an
incremental total *within* each group, and would normally be used with a
control in the detail section therefore. If, as you have, you use this
setting in the case of a control in a group header, as far as I can see it
does appear to count the number of groups, i.e. employees in your case, so
I'm unsure why you are getting inconsistent results.

However, to count the employees you'd normally put the control in the group
header or footer, set the RunningSum property to 'over all' and set the
control's Visible property to False (No in the properties sheet). Put
another text box control in the report footer and set its ControlSource to
reference the hidden control in the group header, so if that's called
txtHiddenCounter say, the ControlSource for the control in the report footer
would be:

=[txHiddenCounter]

The control in the report footer should now show the total number of
employees. If you are still getting inconsistent results, and it is off by
+1 rather than -1 it suggests there is somehow a hidden employee who is being
counted, but why this should be I can't imagine. If its off by -1 that's an
even stranger result.

Another way to do this is by code. Add a text box to the report footer,
txtEmployeeCount say, and leave its ControlSource property blank. In the
report header's Print event procedure initialise the control to zero with:

Me.txtEmployeeCount = 0

In the group header's Print event procedure increment the value of the
control by 1, examining the PrintCount property to avoid any inadvertent
double counting:

If PrintCount = 1 Then
Me.txtEmployeeCount = Me.txtEmployeeCount + 1
End If

One caveat with regard to this method: if you view a report in print preview
and skip over pages with the navigation buttons the Print event for the
skipped pages doesn't fire, so the total will be incorrect. Paging down
through the whole report or sending the whole report to a printer is fine
however. You can avoid this by using the Format event procedures rather than
the Print event procedures, but a Format event procedure can be executed more
than once with the FormatCount being 1 each time, so its more difficult to
predict. Double counting can be undone in the Retreat event procedure, but
it’s a little tricky, and using the Print event procedure is simpler.

BTW if you ever want to aggregate values per page in a page footer, then
doing it in code is the best way, initializing the page footer's control to
zero in the page header's Print event procedure, and then incrementing it in
the detail section's Print event procedure.

Ken Sheridan
Stafford, England
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top