Access messages

T

Tom

Hi All,

I have a report that open only when the query found data.
Here is the code:

Private Sub Report_Open(Cancel As Integer)
If DCount("product", "TotalQuantities") = 0 Then
MsgBox "No data found for this date", vbOKOnly, "Inventory Matrix"
DoCmd.CancelEvent
End If
End Sub

How can I eliminate the Access message: " The OpenReport action was
canceled" ?

I tried DoCmd.set warnnings False, but it didnt work.

TIA,

Tom
 
M

Maurice

The report also has a "On No Data" property. So in code add the following

Private Sub Report_NoData(Cancel As Integer)
Cancel=true
End Sub

Maurice
 
T

Tom

Thanks a lot.
Maurice said:
The report also has a "On No Data" property. So in code add the following

Private Sub Report_NoData(Cancel As Integer)
Cancel=true
End Sub

Maurice
 
T

Tom

Hi,
I still get the access message.
I took your advice and now the cose look like this:

Private Sub Report_NoData(Cancel As Integer)
MsgBox "No data found for this date", vbOKOnly, "Inventory Matrix"
DoCmd.CancelEvent
End Sub

Thanks,

Tom
 
T

Tom Wickerath

Hi Tom,

To add some to Maurice's answer, you might want to include a prompt to the
user to let them know what is going on. Something like this:

Private Sub Report_NoData(Cancel As Integer)
MsgBox "There is no data for the selected criteria.", _
vbInformation, "No Data Available..."
Cancel=true
End Sub


If you are opening your report using a command button on a form, then you
will want to trap for error 2501, as in the following example:

Private Sub cmdPreviewAllItemsbyGroup_Click()
On Error GoTo ProcError

DoCmd.OpenReport "rptAllItemsbyGroup", View:=acViewPreview

ExitProc:
Exit Sub
ProcError:
Select Case Err.Number
Case 2501 'Report cancled.
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, , _
"Error in cmdPreviewAllItemsbyGroup_Click event procedure..."
End Select
Resume ExitProc
End Sub


Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
F

fredg

Hi All,

I have a report that open only when the query found data.
Here is the code:

Private Sub Report_Open(Cancel As Integer)
If DCount("product", "TotalQuantities") = 0 Then
MsgBox "No data found for this date", vbOKOnly, "Inventory Matrix"
DoCmd.CancelEvent
End If
End Sub

How can I eliminate the Access message: " The OpenReport action was
canceled" ?

I tried DoCmd.set warnnings False, but it didnt work.

TIA,

Tom

1) You should use the report's OnNoData event to cancel the report if
there is no data. That's what it is for.

Private Sub Report_NoData(Cancel As Integer)
MsgBox "No data found for this date", vbOKOnly, "Inventory Matrix"
Cancel = True
End Sub

2) Your Open event code is not necessary if you use the OnNoData
event. In any event that shows Cancel as an argument, setting Cancel =
True (as in the OnNoData event) is the same as Do.Cmd.CancelEvent

3) To avoid the "Open Report was canceled" message you need to trap
error 2501 in what ever code event you used to originally open the
report, i.e. a Command Button Click event.

Private Sub CommandName_Click()
On Error GoTo Err_Handler
DoCmd.OpenReport "Report Name", acViewPreview
Exit_This_Sub:
Exit Sub
Err_Handler:
If Err = 2501 Then
Else
MsgBox "Error# " & Err.Number & " " & Err.Description
End If
Resume Exit_This_Sub
End 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