cancel a report preview (1 Viewer)

BEVERLY

New member
Local time
Today, 10:31
Joined
Jan 19, 2001
Messages
5
i have a line to open report from a form:

docmd.openreport 'reportname', acpreview

the report i'm trying to open has a onNoData event procedure and set the Cancel argument to true. this should cancel the opening of the report is empty.

but access sets a run time error....

how can i then cancel my report preview?
 

Talismanic

Registered User.
Local time
Today, 10:31
Joined
May 25, 2000
Messages
377
Beverly,

You can use a DCount to check for data in your table. If there is none the event can be canceled. The syntax for a DCount is the same as the Syntax for a DLookUp.

Here is an example of a DCount that checks for a valid name in a table before it will open.

Private Sub cmdReport_Click()

Dim Answer As Variant
Dim cancel As Integer

If DCount("[Name]", "YourTable", "[Name]= '" & Me!txtName & "'") = 0 Then

Answer = MsgBox("Name is not on the list", vbOKOnly, "Hey You!")
If Answer = vbOK Then cancel = True

Exit Sub

End If

DoCmd.OpenReport "ReportName", acPreview

End Sub


Basicaly the DCount looks at the table for the requested record if it = 0 the event is canceled or if it =>1 the event continues and the report will be opened.

Post back if you have more questions!

[This message has been edited by Talismanic (edited 01-26-2001).]
 
R

Richie

Guest
Change the error handling of your command button to
Exit_Preview_Click:
Exit Sub

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

BEVERLY

New member
Local time
Today, 10:31
Joined
Jan 19, 2001
Messages
5
thanks to u both..

i've tried the Dcount approach and find it ammm, just consuming... i did the on error approach and i guess this single line saved me.. thanks...
 

deekras

Registered User.
Local time
Today, 10:31
Joined
Jun 14, 2000
Messages
169
i am looking to do the same thing. where would i put that coding? and could you explain it more please?
 

BEVERLY

New member
Local time
Today, 10:31
Joined
Jan 19, 2001
Messages
5
'before calling the openreport command

On Error Resume Next 'this line let's you pass through the error 2501 generated by the
'on no data event procedure when calling the report
'and setting cancel to true

DoCmd.OpenReport [report name], acViewNormal
 

Users who are viewing this thread

Top Bottom