Full Version: 0 Records return popup msg box
UtterAccess Discussion Forums > Microsoft® Access > Access Reports
Al3x
is there a way to popup a box when there are no records returned from a query when you wish to create a report?

such as "there are no records in this report" so you dont see the preview
R. Hicks
Use the On No Data event of the report to do this ...

RDH
Al3x
cheers rick for pointing it out, i never knew there were this feature frown.gif
R. Hicks
You are Welcome ... wink.gif

RDH
Al3x
Sorry to bother you again but how do i code it to display a msg box and NOT display the report? i got it displaying the msg box but the report appears!
R. Hicks
Add a line in the On No Data event ...
CODE
Cancel = True

RDH
Al3x
that worked but i get an access error sayign the access report open was cancelled, is there any way of displaying a nicer error message after my msg box?
R. Hicks
Post the code that you are using to open the report ...

RDH
Al3x
just the basic ms access wizard:

Private Sub cmdCompReport_Click()
On Error GoTo Err_cmdCompReport_Click

Dim stDocName As String

stDocName = "rptCompReport"
DoCmd.OpenReport stDocName, acPreview

Exit_cmdCompReport_:
Exit Sub

Err_cmdCompReport_Click:
MsgBox Err.Description
Resume Exit_cmdCompReport_

End Sub

Then in my report i have:

Private Sub Report_NoData(Cancel As Integer)
MsgBox "No Data In Report."
Cancel = True
End Sub

Also i have an issue because i have the form on popup the preview report appears behind?
R. Hicks
Try altering to this:
CODE
Private Sub cmdCompReport_Click()
On Error GoTo Err_cmdCompReport_Click

Dim stDocName As String

stDocName = "rptCompReport"
DoCmd.OpenReport stDocName, acPreview

Exit_cmdCompReport_Click:
  Exit Sub

Err_cmdCompReport_Click:
  If err.Number <> 2501 Then
    MsgBox Err.Description
  End if

  Resume Exit_cmdCompReport_Click

End Sub

RDH
Al3x
that did the trick but the report is still at the back of the popup form box? is there anyway of bringing it forward?
R. Hicks
Either close the form ... or set the form to be hidden when the report opens ...

RDH
Al3x
the fix works fine ricky but what exactly does it do? i dont understand the code
R. Hicks
I hope I understand what you are asking ....

If the form is set as popup and/or modal ...
Then the get the another object to appear in front of it .. you ether have to close the form or set it's visible property to be hidden ...

If this is not what you are talking about .. explain what code ??

RDH
Al3x
sorry i said the wrong thing i meant this code:

CODE
    If err.Number <> 2501 Then    
MsgBox Err.Description  
End if
R. Hicks
Oh ...

If the user or Access Cancel an operation ... Access will generate the error number 2501.

So the code basically says:
If the error number "is not" 2501 .. display a messagebox to the user to expolaining what may have happened.
Then exit the routine ..

But if the error "is" 2501 .. just exit the routine without any message to the user ...

I hoped I have explained it well enough for you ...

RDH
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.