How do I call a function from the On Print event of a Sub Report

G

Guest

My problem is when I place a sub report into a main report I am receiving
error message when the main report opens :
"The expression On Print you entered as the event property setting produced
the following error: Type Mismatch.
*The expression may not result in the name of a macro, the
name of a user defined function, or [event procedure
I am trying to call these functions in a Standard module, from the On Print
Event of a Subreport
Group property
=SetCount([Reports]![srptReport1])
Detail property
=PrintLines([Reports]![srptReport1],[TotGrp])
 
A

Allen Browne

Is srptReport1 the name of the subreport? If so, try:
=SetCount([Report])

Subforms and subreports are not open in their own right, i.e. they are not
part of the Forms or Reports collection. You could refer to them through the
main report, e.g.:
[Reports]![Report1]![srptReport1].Report
but the full reference is not necessary since the subreport knows its own
context.

We are assuming here that the SetCount() function is set up to accept a
Report argument, e.g.:
Function SetCount(rpt As Report)
 
G

Guest

In the code below you can see R is passed to the function. So I still am not
sure how to make the Main report use these functions for each sub report. My
goal is to have each grouping on both subreports have the same number of
lines for each section. The reports are unrelated and parallel on the page
and the sections sizes need to match no matter how many records in the group.
Here is the code in a Standard Module
Function SetCount(R As Report)
TotCount = 0
R![Entry_Description].Visible = True
R![Entry_Amt].Visible = True
End Function
' Call the SetCount() function from the group header section's
' OnPrint property using the syntax: =SetCount(Report)

Here is the code for the function to print each 15 lines for each group in
the subreports.


Function PrintLines(R As Report, TotGrp)
TotCount = TotCount + 1
If TotCount = TotGrp Then
R.NextRecord = False
ElseIf TotCount > TotGrp And TotCount < 15 Then
R.NextRecord = False
R![Entry_Description].Visible = False
R![Entry_Amt].Visible = False
End If
End Function
Thanks

Allen Browne said:
Is srptReport1 the name of the subreport? If so, try:
=SetCount([Report])

Subforms and subreports are not open in their own right, i.e. they are not
part of the Forms or Reports collection. You could refer to them through the
main report, e.g.:
[Reports]![Report1]![srptReport1].Report
but the full reference is not necessary since the subreport knows its own
context.

We are assuming here that the SetCount() function is set up to accept a
Report argument, e.g.:
Function SetCount(rpt As Report)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

JoeA2006 said:
My problem is when I place a sub report into a main report I am receiving
error message when the main report opens :
"The expression On Print you entered as the event property setting
produced
the following error: Type Mismatch.
*The expression may not result in the name of a macro, the
name of a user defined function, or [event procedure
I am trying to call these functions in a Standard module, from the On
Print
Event of a Subreport
Group property
=SetCount([Reports]![srptReport1])
Detail property
=PrintLines([Reports]![srptReport1],[TotGrp])
 
A

Allen Browne

From your original post, I take it that this all works if you open the
subreport as a stand-alone report, but that you are getting a "Type
mismatch" if you open it as a subreport.

The functions are actually being called by the subreport itself, so no
reference to the main report is needed. Try replacing:
=SetCount([Reports]![srptReport1])
with
=SetCount([Report])
and replace:
=PrintLines([Reports]![srptReport1],[TotGrp])
with:
=PrintLines([Report], [TotGrp])

Then open the subreport itself, and see that the reference still works. If
it does, this change should also work as a subreport.

If it does not work, what error message are you receiving?

Another way to get it to work would be to change the property in the
subreport from:
=SetCount([Reports]![srptReport1])
to:
[Event Procedure]
Then click the Build button (...) beside this.
Access opens the code window.
Between the "Private Sub..." and "End Sub" lines, enter:
Call SetCount(Me)

Then do the same with the other event procedure, inserting the code line:
Call PrintLines(Me, TotGrp)

(It is not clear to me what TotGrp is, but it should work if it works as a
stand-alone report.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

JoeA2006 said:
In the code below you can see R is passed to the function. So I still am
not
sure how to make the Main report use these functions for each sub report.
My
goal is to have each grouping on both subreports have the same number of
lines for each section. The reports are unrelated and parallel on the page
and the sections sizes need to match no matter how many records in the
group.
Here is the code in a Standard Module
Function SetCount(R As Report)
TotCount = 0
R![Entry_Description].Visible = True
R![Entry_Amt].Visible = True
End Function
' Call the SetCount() function from the group header section's
' OnPrint property using the syntax: =SetCount(Report)

Here is the code for the function to print each 15 lines for each group in
the subreports.


Function PrintLines(R As Report, TotGrp)
TotCount = TotCount + 1
If TotCount = TotGrp Then
R.NextRecord = False
ElseIf TotCount > TotGrp And TotCount < 15 Then
R.NextRecord = False
R![Entry_Description].Visible = False
R![Entry_Amt].Visible = False
End If
End Function
Thanks

Allen Browne said:
Is srptReport1 the name of the subreport? If so, try:
=SetCount([Report])

Subforms and subreports are not open in their own right, i.e. they are
not
part of the Forms or Reports collection. You could refer to them through
the
main report, e.g.:
[Reports]![Report1]![srptReport1].Report
but the full reference is not necessary since the subreport knows its own
context.

We are assuming here that the SetCount() function is set up to accept a
Report argument, e.g.:
Function SetCount(rpt As Report)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

JoeA2006 said:
My problem is when I place a sub report into a main report I am
receiving
error message when the main report opens :
"The expression On Print you entered as the event property setting
produced
the following error: Type Mismatch.
*The expression may not result in the name of a macro,
the
name of a user defined function, or [event procedure
I am trying to call these functions in a Standard module, from the On
Print
Event of a Subreport
Group property
=SetCount([Reports]![srptReport1])
Detail property
=PrintLines([Reports]![srptReport1],[TotGrp])
 
G

Guest

I tried the first suggestion and it worked. I will leave it at that
Thanks.
Allen Browne said:
From your original post, I take it that this all works if you open the
subreport as a stand-alone report, but that you are getting a "Type
mismatch" if you open it as a subreport.

The functions are actually being called by the subreport itself, so no
reference to the main report is needed. Try replacing:
=SetCount([Reports]![srptReport1])
with
=SetCount([Report])
and replace:
=PrintLines([Reports]![srptReport1],[TotGrp])
with:
=PrintLines([Report], [TotGrp])

Then open the subreport itself, and see that the reference still works. If
it does, this change should also work as a subreport.

If it does not work, what error message are you receiving?

Another way to get it to work would be to change the property in the
subreport from:
=SetCount([Reports]![srptReport1])
to:
[Event Procedure]
Then click the Build button (...) beside this.
Access opens the code window.
Between the "Private Sub..." and "End Sub" lines, enter:
Call SetCount(Me)

Then do the same with the other event procedure, inserting the code line:
Call PrintLines(Me, TotGrp)

(It is not clear to me what TotGrp is, but it should work if it works as a
stand-alone report.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

JoeA2006 said:
In the code below you can see R is passed to the function. So I still am
not
sure how to make the Main report use these functions for each sub report.
My
goal is to have each grouping on both subreports have the same number of
lines for each section. The reports are unrelated and parallel on the page
and the sections sizes need to match no matter how many records in the
group.
Here is the code in a Standard Module
Function SetCount(R As Report)
TotCount = 0
R![Entry_Description].Visible = True
R![Entry_Amt].Visible = True
End Function
' Call the SetCount() function from the group header section's
' OnPrint property using the syntax: =SetCount(Report)

Here is the code for the function to print each 15 lines for each group in
the subreports.


Function PrintLines(R As Report, TotGrp)
TotCount = TotCount + 1
If TotCount = TotGrp Then
R.NextRecord = False
ElseIf TotCount > TotGrp And TotCount < 15 Then
R.NextRecord = False
R![Entry_Description].Visible = False
R![Entry_Amt].Visible = False
End If
End Function
Thanks

Allen Browne said:
Is srptReport1 the name of the subreport? If so, try:
=SetCount([Report])

Subforms and subreports are not open in their own right, i.e. they are
not
part of the Forms or Reports collection. You could refer to them through
the
main report, e.g.:
[Reports]![Report1]![srptReport1].Report
but the full reference is not necessary since the subreport knows its own
context.

We are assuming here that the SetCount() function is set up to accept a
Report argument, e.g.:
Function SetCount(rpt As Report)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

My problem is when I place a sub report into a main report I am
receiving
error message when the main report opens :
"The expression On Print you entered as the event property setting
produced
the following error: Type Mismatch.
*The expression may not result in the name of a macro,
the
name of a user defined function, or [event procedure
I am trying to call these functions in a Standard module, from the On
Print
Event of a Subreport
Group property
=SetCount([Reports]![srptReport1])
Detail property
=PrintLines([Reports]![srptReport1],[TotGrp])
 

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