Solved Report to show "NO DATA" (1 Viewer)

Weekleyba

Registered User.
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
586
I have a report that opens in acViewReport from a command button on a form.
When the report shows no data from the underlying query, I want the text "NO DATA" to appear.
Currently I have a label showing "NO DATA" with the Visible property set to No.
In the report's On Current event, I put the following code:
Code:
Private Sub Report_Current()
'To show or not show No Data label.
    Me.ProjectTitle.SetFocus
    
    If IsNull(GrandTotal) Or GrandTotal = "" Then
        Me.lblNO_DATA.Visible = True
    End If
    
End Sub

The problem is, it will not show the "NO DATA" until I click on the report.
How do I fix that?

Before clicking on the report.
1688673345048.png


After clicking on the report.
1688673617366.png


1688673785740.png
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:27
Joined
Sep 21, 2011
Messages
14,306
There is a NoData event in a report. Why not use that?
1688675292847.png
 

isladogs

MVP / VIP
Local time
Today, 13:27
Joined
Jan 14, 2017
Messages
18,225
The standard approach is to put a message box in the Report_NoData event.
 

Weekleyba

Registered User.
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
586
Thanks for the solution.
Here's what I put in the On No Data event of the report.
Code:
Private Sub Report_NoData(Cancel As Integer)
'If no data in the query, just gives message box.
    
    MsgBox "No records to display."
    Cancel = True
    
End Sub
 

isladogs

MVP / VIP
Local time
Today, 13:27
Joined
Jan 14, 2017
Messages
18,225
f you are running the report from code in a form, you will also need to handle error 2501 in the form event
 

GPGeorge

Grover Park George
Local time
Today, 05:27
Joined
Nov 25, 2004
Messages
1,867
Private Sub cmdPrint_Click()

On Error GoTo ErrHandler

Call CreateReport(intViewType:=acViewNormal)

ExitProc:

Exit Sub

ErrHandler:

If Err.Number <> 2501 Then
MsgBox Err & " " & Err.Description
End If
Resume ExitProc
Resume

End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:27
Joined
Sep 21, 2011
Messages
14,306
Thanks for the solution.
Here's what I put in the On No Data event of the report.
Code:
Private Sub Report_NoData(Cancel As Integer)
'If no data in the query, just gives message box.
   
    MsgBox "No records to display."
    Cancel = True
   
End Sub
So why not set the label visibility to True ?
 

Users who are viewing this thread

Top Bottom