Kefash
Kefash

Reputation: 533

problems with DoCmd.OpenReport

I have a button on a form that opens a report. The report open as it should whenever there is data however when there is no data in the report I get an error and the debugger takes me to this line
DoCmd.OpenReport "FilesToBeReturnedReport", acViewReport, "", "", acNormal

Here is the entire code:

`With Me
    If ((IsNull(.StartDate) Or .EndDate = "")) Then
        Beep
        MsgBox "You must enter report start period", vbOKOnly, "Required Data!"
    ElseIf (IsNull(.EndDate) Or .EndDate = "") Then
        Beep
        MsgBox "You must enter report end date", vbOKOnly, "Reqired Data"
    ElseIf (.StartDate > .EndDate) Then
        Beep
        MsgBox "The report start date must be earlier than the end date", vbOKOnly, "Required Data"
    Else
        DoCmd.OpenReport "FilesToBeReturnedReport", acViewReport, "", "", acNormal
    End If
End With`

I need to complete this by tomorrow so any help will be much appreciated. Thanks

Upvotes: 2

Views: 3072

Answers (2)

pteranodon
pteranodon

Reputation: 2059

Access reports have a NoData event. You can put the actions you want to run in the case of no data there. If you set Cancel to True inside the NoData event, the report will not open or print. If you leave Cancel as its default False, the report will open as it does now.

That said, you also need to catch this error when using DoCmd.OpenReport:

On Error Goto ReportFail
DoCmd.OpenReport strReport, acViewPreview

ExitHere:
Exit Sub

ReportFail:
If Err=2501 Then
  'OpenReport was canceled
  Err.Clear
  Resume ExitHere
Else
  MsgBox Err.Number & ": " & Err.Description
End If
End Sub

Upvotes: 2

Dre488
Dre488

Reputation: 23

I'm not really extremely experienced with vba in access but why don't you try puttting

DoCmd.OpenReport "FilesToBeReturnedReport", acViewReport, "", "", acNormal

after the

End with

Upvotes: -1

Related Questions