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
fredg - 19 Apr 2008 17:39 GMT
> A97
>
[quoted text clipped - 28 lines]
>
> 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

Signature
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
Chuck - 19 Apr 2008 23:57 GMT
Thank you Fred. Works perfectly.
Chuck
>> A97
>>
[quoted text clipped - 53 lines]
> End If
> Resume Exit_Sub