Counting records in a field in a report

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I know this must be simple, but I can't seem to find how to do it anywhere.
I want to count how many students are in a report at the bottom of the
report. I've tried using =Count([Student Name]) but I'm missing something.
 
It depends on whether there is one detail row per student in the report or
not. If there is then put a text box in the report footer with a
ControlSource pf :

=Count(*)

If there are multiple rows per student, e.g. if it includes multiple classes
attended by each student then one way you can count the students is in code
after grouping the report by student via the sorting and grouping dialogue on
design view and adding an unbound text box, txtStudentCount say, to the
report footer, leaving its ControlSource blank. Group the report first on
Student Name but don't give this group a group footer as its simply to order
the report by student name (assuming that's what's wanted) . Then group by
StudentID or whatever field uniquely identifies the students and include a
group header for the StudentID group. You don't necessarily need to include
any controls in the header but if you don't set its height to marginally more
than zero or its print event won't fire.

In the report header's Print event procedure initialise the txtStudentCount
text box to zero:

Me.txtStudentCount = 0

In the Print event procedure of the StudentID group header put code to
increment the txtStudentCount text box's value by 1,examining the
PrintCount property to avoid any inadvertent double counting:

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

You might have realized by now that this method of counting can also be used
for conditional counting. Say you wanted to count the male and female
students separately and show the results in two text boxes in the report
footer:

If PrintCount = 1 Then
If Me.Gender = "M" Then
Me.txtMaleStudentCount = Me.txtMaleStudentCount + 1
Else
Me.txtFemaleStudentCount = Me.txtFemaleStudentCount + 1
End If
End If

Extending this example lets say you wanted, this time using code in the
Print event procedure of the detail section of a report which lists all
students' marks in exams, to total the marks separately for male and female
students then you can do this by incrementing the controls by the current
mark multiplied by the return value of an expression which returns 0 or 1,
using the IIf function for this:

If PrintCount = 1 Then
Me.txtMaleStudentMarks = _
Me.txtMaleStudentMarks + (Me.Marks * IIf(Gender="M",1,0))
Me.txtFemaleStudentMarks = _
Me.txtFemaleStudentMarks + (Me.Marks * IIf(Gender="F",1,0))
End If
End If

One caveat: using the Print event procedure like this means the results
won't show correctly if you skip over pages in print preview as the Print
events for those pages won't fire. Paging down through all pages or sending
the whole report to the printer will be fine, however.

Ken Sheridan
Stafford, England

SherryLaine said:
I know this must be simple, but I can't seem to find how to do it anywhere.
I want to count how many students are in a report at the bottom of the
report. I've tried using =Count([Student Name]) but I'm missing something.
 
Thank you, thank you, thank you! I knew it was simple... it was the location
of the text box. I wasn't putting it in the report footer!

Ken Sheridan said:
It depends on whether there is one detail row per student in the report or
not. If there is then put a text box in the report footer with a
ControlSource pf :

=Count(*)

If there are multiple rows per student, e.g. if it includes multiple classes
attended by each student then one way you can count the students is in code
after grouping the report by student via the sorting and grouping dialogue on
design view and adding an unbound text box, txtStudentCount say, to the
report footer, leaving its ControlSource blank. Group the report first on
Student Name but don't give this group a group footer as its simply to order
the report by student name (assuming that's what's wanted) . Then group by
StudentID or whatever field uniquely identifies the students and include a
group header for the StudentID group. You don't necessarily need to include
any controls in the header but if you don't set its height to marginally more
than zero or its print event won't fire.

In the report header's Print event procedure initialise the txtStudentCount
text box to zero:

Me.txtStudentCount = 0

In the Print event procedure of the StudentID group header put code to
increment the txtStudentCount text box's value by 1,examining the
PrintCount property to avoid any inadvertent double counting:

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

You might have realized by now that this method of counting can also be used
for conditional counting. Say you wanted to count the male and female
students separately and show the results in two text boxes in the report
footer:

If PrintCount = 1 Then
If Me.Gender = "M" Then
Me.txtMaleStudentCount = Me.txtMaleStudentCount + 1
Else
Me.txtFemaleStudentCount = Me.txtFemaleStudentCount + 1
End If
End If

Extending this example lets say you wanted, this time using code in the
Print event procedure of the detail section of a report which lists all
students' marks in exams, to total the marks separately for male and female
students then you can do this by incrementing the controls by the current
mark multiplied by the return value of an expression which returns 0 or 1,
using the IIf function for this:

If PrintCount = 1 Then
Me.txtMaleStudentMarks = _
Me.txtMaleStudentMarks + (Me.Marks * IIf(Gender="M",1,0))
Me.txtFemaleStudentMarks = _
Me.txtFemaleStudentMarks + (Me.Marks * IIf(Gender="F",1,0))
End If
End If

One caveat: using the Print event procedure like this means the results
won't show correctly if you skip over pages in print preview as the Print
events for those pages won't fire. Paging down through all pages or sending
the whole report to the printer will be fine, however.

Ken Sheridan
Stafford, England

SherryLaine said:
I know this must be simple, but I can't seem to find how to do it anywhere.
I want to count how many students are in a report at the bottom of the
report. I've tried using =Count([Student Name]) but I'm missing something.
 
Back
Top