PreviewTwoPages only when data is present (1 Viewer)

moose

c'mon Chelsea
Local time
Today, 06:54
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?
 

FoFa

Registered User.
Local time
Today, 00:54
Joined
Jan 29, 2003
Messages
3,672
You could run a dcount in the condition section of the macro and only run the report if a greater than zero condition exists
 

moose

c'mon Chelsea
Local time
Today, 06:54
Joined
May 18, 2001
Messages
139
sorry for being so ignorant but could you please explain what i have to do and where i have to put it
 
R

Rich

Guest
Scrap the macro, use code and the no data event of the report to cancel the event
 

moose

c'mon Chelsea
Local time
Today, 06:54
Joined
May 18, 2001
Messages
139
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
 

SforSoftware

(NL) SnelStart specialist
Local time
Today, 07:54
Joined
Aug 16, 2003
Messages
239
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.
 
R

Rich

Guest
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
 

moose

c'mon Chelsea
Local time
Today, 06:54
Joined
May 18, 2001
Messages
139
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: 156

Users who are viewing this thread

Top Bottom