Problem opening 2 reports

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

Guest

I am opening 2 reports on the press of one button on a form.
The problem I have is how to handle things if either of the reports has no
data.
Using On NoData to display a message and cancel the report works fine if
just one report is opened but when both are opened together (which they need
to be to avoid another button press) I get a problem as cancelling closes
both reports.
Can anyone offer advice?
 
Hi.
The problem I have is how to handle things if either of the reports has no
data.

Create a public subroutine in a module that opens the one report passed to
it. Your button's OnClick( ) event will call this subroutine once for each
report. That way, if one report is cancelled, it's the only report being
handled by that subroutine's error handler.

In the form with the button that opens the two reports, try:

' * * * * Start Code * * * *

Private Sub Open2RptsBtn_Click()

On Error GoTo ErrHandler

Call OpenRpt("rptMyReport")
Call OpenRpt("rptMyOtherReport")

Exit Sub

ErrHandler:

MsgBox "Error in Open2RptsBtn_Click( ) in " & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description

Err.Clear

End Sub

' * * * * End Code * * * *

Paste the following code in a module:

' * * * * Start Code * * * *

Public Sub OpenRpt(sRptName As String)

On Error GoTo ErrHandler

DoCmd.OpenReport sRptName, acViewPreview

Exit Sub

ErrHandler:

If (Err.Number <> 2501) Then ' Report not cancelled.

MsgBox "Error in OpenRpts( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
End If

Err.Clear

End Sub

' * * * * End Code * * * *


HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail harvesters
for spammers are (e-mail address removed) and (e-mail address removed)

- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts as "Answers,"
so that all may benefit by filtering on "Answered questions" and quickly
finding the right answers to similar questions. Remember that questions
answered the quickest are often from those who have a history of rewarding
the contributors who have taken the time to answer questions correctly.
 
Thanks, this has pretty much solved the problem.
Just need to sort how or where to put the restore comand as the reports are
maximised on opening. I have my restore command in the On Close of the first
report to be opened which appears second but when there is no data in this
report the restore happens too early as the report is cancelled immediately.
Once the other report is close there is no restore as that would restore too
early when there was data in both.
I guess one solution would be to specific the size of the reports rather
than telling them to maximise then the restore wouldn't be needed.
 
Hi.

You might want to have your code check whether the other report is still
open before restoring the size. In each report's OnClose( ) event, try:

' * * * * Start Code * * * *

Private Sub Report_Close()

On Error GoTo ErrHandler

If (SysCmd(acSysCmdGetObjectState, acReport, "rptMyOtherReport") = 0)
Then
DoCmd.Restore
End If

Exit Sub

ErrHandler:

MsgBox "Error in Report_Close( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Sub

' * * * * End Code * * * *

Replace the name of this example report with the name of the _other_ report
that may or may not be open.

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail harvesters
for spammers are (e-mail address removed) and (e-mail address removed)
 
Many thanks this worked too.

'69 Camaro said:
Hi.

You might want to have your code check whether the other report is still
open before restoring the size. In each report's OnClose( ) event, try:

' * * * * Start Code * * * *

Private Sub Report_Close()

On Error GoTo ErrHandler

If (SysCmd(acSysCmdGetObjectState, acReport, "rptMyOtherReport") = 0)
Then
DoCmd.Restore
End If

Exit Sub

ErrHandler:

MsgBox "Error in Report_Close( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Sub

' * * * * End Code * * * *

Replace the name of this example report with the name of the _other_ report
that may or may not be open.

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail harvesters
for spammers are (e-mail address removed) and (e-mail address removed)
 
You're welcome. Glad to help.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail harvesters
for spammers are (e-mail address removed) and (e-mail address removed)
 
Back
Top