Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Frederick, Maryland, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Blank Report (A2K SR1)

    I know this question has been answered before, but I can't find the post.

    I have a form with a 'Generate Report' button. After selecting a particular company from a combo box on the form, the user presses the Generate Report button, opening a report based on the selected company. This works fine. However, I'm looking for a simple way to handle instances where a particular company has no data for the report. In the report's NoData event, I've added a message box, but after acknowledging the message, the blank report opens anyway. I've tried adding DoCmd.Close commands in various places, but I can't get it to work right.

    Ideally, after the user presses the 'Generate Report' button, if there are no report data, I'd like the 'Sorry, there's no data...' message box to appear and after acknowledging the message, the user is back at the company selection form.

    I apologize if this is confusing and I should be able to figure this out. Thanks for the help.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Blank Report (A2K SR1)

    Have a look at the thread starting at <!post=post 150885, 150885>post 150885<!/post>.

  3. #3
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Frederick, Maryland, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Blank Report (A2K SR1)

    Here is the code behind the 'Generate Report' button on the popup form in which the user selects the Company to generate the report for:

    Private Sub SubAnalReport_Click()
    'Open the report
    DoCmd.OpenReport "rptSUBANALYSISREPORT", acViewPreview
    'Close the popup form
    DoCmd.Close acForm, "frmSUBANALYSISREPORT"
    End Sub

    Here is the code in the On No Data event of the report:

    Private Sub Report_NoData(Cancel As Integer)
    MsgBox "There is no data for this subcontractor. No report was generated."
    Cancel = True
    End Sub

    If I run the report NOT USING the popup form and there is no data for the Company, the code works as it should...it presents the message box and closes the form. However, when I run the report USING the popup form and there is no Company data, I'm presented with a Visual Basic error dialog that states:

    "Run-time error '2501': The OpenReport action was cancelled."

    Obviously, the report is not running because there is no data. I just can't figure out how to 'trap' that event and avoid the VBA error dialog box.

  4. #4
    3 Star Lounger
    Join Date
    Dec 2001
    Location
    Nevada, USA
    Posts
    207
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Blank Report (A2K SR1)

    I have a similar situation and have used the On Error statement to trap the error. Look in VB Help under On Error. There is a very good example of the use of this statement in error trapping.

    HTH.
    Carol W.

  5. #5
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Blank Report (A2K SR1)

    All you have to do is test for Err = 2501 in the On Error trap routine and bypass the msgbox that tells you all about it.
    HTH
    Pat

  6. #6
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Frederick, Maryland, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Blank Report (A2K SR1)

    Thanks, everyone. The solution was beautifully simple. Here's what I did:

    1. In the On No Data event of the report, I inserted only this code: Cancel = True
    2. In the On Click event of the Generate Report button on my form, I inserted this code:

    Private Sub SubAnalReport_Click()
    On Error GoTo ErrorHandler
    DoCmd.OpenReport "rptSUBANALYSISREPORT", acViewPreview 'Opens the report based on the subcontractor selected from the combo box
    DoCmd.Close acForm, "frmSUBANALYSISREPORT" 'Closes the subcontractor selection form
    On Error GoTo 0
    Err.Clear
    Exit Sub

    ErrorHandler:
    Select Case Err.Number
    Case 2501
    MsgBox "A report was not created because there was no data for this subcontractor."
    Me!Company = ""
    End Select
    End Sub

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •