Close label report if no data selected

C

Chuck

A97

I have a form with a command button to open a label report

The label report is based on a query. If the result of the query has no data
because the criteria was not met, how can I prevent the label report from
opening?

The label report module has the following statements:

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
On Error GoTo Err_Detail_Print

(Snip special formatting)

Err_Detail_Print:
MsgBox "No Labels Selected To Print", vbOKOnly, "Label Selection Problem"
DoCmd.Close acDefault, acSaveNo
Exit Sub
End Sub

The message box opens, I click OK, The message box closes and the report
opens. The three fields (name, address, city-state-zip) all show #ERROR.
This doesn't hurt anything, it just looks bad. I would like the report to not
open at all and the focus go back the form with the command button to open the
report.

I have tried: DoCmd.Close acReport, "Avery 8160 Labels", acSaveNo
This produced an error message: This action can not be carried out while
processing a form or report event

Chuck
 
F

fredg

A97

I have a form with a command button to open a label report

The label report is based on a query. If the result of the query has no data
because the criteria was not met, how can I prevent the label report from
opening?

The label report module has the following statements:

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
On Error GoTo Err_Detail_Print

(Snip special formatting)

Err_Detail_Print:
MsgBox "No Labels Selected To Print", vbOKOnly, "Label Selection Problem"
DoCmd.Close acDefault, acSaveNo
Exit Sub
End Sub

The message box opens, I click OK, The message box closes and the report
opens. The three fields (name, address, city-state-zip) all show #ERROR.
This doesn't hurt anything, it just looks bad. I would like the report to not
open at all and the focus go back the form with the command button to open the
report.

I have tried: DoCmd.Close acReport, "Avery 8160 Labels", acSaveNo
This produced an error message: This action can not be carried out while
processing a form or report event

Chuck

This is what the report's OnNoData event is for.
You can get rid of your Detail_Print code.

Code the Report's OnNoData event:

MsgBox "No records were found."
Cancel = True


The above will raise an error if the report was opened from code,
so...
Also, code the form command button event that is used to open the
report:

On Error GoTo Err_Handler
DoCmd.OpenReport "YourReport", acViewPreview

Exit_Sub:
Exit Sub
Err_Handler:
If Err = 2501 Then
Else
MsgBox "Error #: " & Err.Number & " " & err.Desription
End If
Resume Exit_Sub
 
C

Chuck

Thank you Fred. Works perfectly.

Chuck
This is what the report's OnNoData event is for.
You can get rid of your Detail_Print code.

Code the Report's OnNoData event:

MsgBox "No records were found."
Cancel = True


The above will raise an error if the report was opened from code,
so...
Also, code the form command button event that is used to open the
report:

On Error GoTo Err_Handler
DoCmd.OpenReport "YourReport", acViewPreview

Exit_Sub:
Exit Sub
Err_Handler:
If Err = 2501 Then
Else
MsgBox "Error #: " & Err.Number & " " & err.Desription
End If
Resume Exit_Sub
 

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