PreviewTwoPages only when data is present

moose

c'mon Chelsea
Local time
Today, 04:14
Joined
May 18, 2001
Messages
139
I have a Macro that opens a report in TwoPagePreview that works fine so long as there is data to show.
In the no data event of the report is another Macro that opens an ok/cancel type of form.
The problem is when there is no data the run command in the macro still trys to open the report in TwoPagePreview and falls over because the report is not open.
How can i get the run command to work only when there is data to be displayed in the report?
 
You could run a dcount in the condition section of the macro and only run the report if a greater than zero condition exists
 
sorry for being so ignorant but could you please explain what i have to do and where i have to put it
 
Scrap the macro, use code and the no data event of the report to cancel the event
 
i wouldn't know where to start with the code to do that, which is why i tried to use macros. I know it would be better to use code

Any help would be appreciated
 
Hi,

When you put a condition before the action in your macro, you can count the number of items in the recordsource of your report (when it's a query). I.e.:
DCount("*";"QueryReport")>0

QueryReport is the name of the query on which your report is based. Now you say that the Count of items in the query must be more than 0 before the RunCommand may run.
 
Private Sub Preview_Click()
Dim strDocName As String
On Error GoTo Err_Preview_Click
strDocName = "YourReport"
DoCmd.OpenReport strDocName, acViewPreview
DoCmd. RunCommand acCmdPreviewTwoPages

Exit_Preview_Click:
Exit Sub

Err_Preview_Click:
If Err = 2501 Then
Resume Exit_Preview_Click
Else
MsgBox Err.Description
Resume Exit_Preview_Click
End If

End Sub
In the On NoData event of the report put
Cancel = True
 
i have this code now

Private Sub Preview_Click()
Dim strDocName As String
On Error GoTo Err_Preview_Click
strDocName = "concession"
DoCmd.OpenReport strDocName, acViewPreview
DoCmd.RunCommand acCmdPreviewTwoPages

Exit_Preview_Click:
Exit Sub

Err_Preview_Click:
If Err = 2501 Then
Resume Exit_Preview_Click
Else
MsgBox "There are no records matching your search criteria"
Resume Exit_Preview_Click
End If

and in the nodata event

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

but i get an error in the initial macro that is behind a command button that runs the report. I cant change the macro because it is the 'PreviewTwoPages' part that it falls over on.
i have attached a screen print of the error
 

Attachments

  • error msg.jpg
    error msg.jpg
    47.7 KB · Views: 195

Users who are viewing this thread

Back
Top Bottom