Results 1 to 4 of 4
  1. #1
    Lounger
    Join Date
    Dec 2003
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    close all reports ? (Access 2000)

    Is there any function to close all reports when closing a report?
    In the OnClose event of one of my open repots i have put the
    following code:
    Public Function CloseAllReports()
    Dim i As Integer
    For i = Reports.Count - 1 To 0 Step -1
    DoCmd.Close acReport, Reports(i).Name
    Next i
    Beep
    End Function
    I do receive however the following mesage:

    The action cant be carried out while a form or report is being opened.
    I suppose my code is not the right one.

  2. #2
    Star Lounger
    Join Date
    Mar 2001
    Location
    Dudley, Midlands, England
    Posts
    55
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: close all reports ? (Access 2000)

    I think your problem might be that Access doesn't like you trying again to close the current report when it's already proccessing the OnClose event for it.

    You could try something like If Reports(i).Name <> Me.Name Then DoCmd.Close acReport, Reports(i).Name

    On a similar basis, you might want to watch out for problems if you include the code in each report's OnClose event. When you close report A and it tries to close report B, report B's OnClose event will fire and that will start trying to work its way through the open reports too.

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: close all reports ? (Access 2000)

    If this code is in the report being closed, you can get around the problem by modifying your code as follows;

    Public Function CloseAllReports()
    Dim i As Integer
    For i = Reports.Count - 1 To 0 Step -1
    If Reports(i).Name <> Me.Name Then
    DoCmd.Close acReport, Reports(i).Name
    End If
    Next i
    Beep
    End Function

    If this is a function being called in a standard module, pass it the current report object (the one being closed) as an argument like this:

    Public Function CloseAllReports(rpt As Report)
    Dim i As Integer
    For i = Reports.Count - 1 To 0 Step -1
    If Reports(i).Name <> rpt.Name Then
    DoCmd.Close acReport, Reports(i).Name
    End If
    Next i
    Beep
    End Function


    And call it in the close event of the report like this:

    CloseAllReports Me
    Charlotte

  4. #4
    Star Lounger
    Join Date
    Mar 2001
    Location
    Dudley, Midlands, England
    Posts
    55
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: close all reports ? (Access 2000)

    There is indeed a further problem, though, if you're intending to include this code (or the call to this code) in every report. If this is being used only in a "master" report then Charlotte's code does the business.

    As I suggested yesterday (but at that point hadn't tried it), report A errors trying to close report A, so the added "If" statement will avoid that eventuality. But when report A closes report B, report B's own OnClose event will kick in and the code that calls will hit the error condition not only when it tries to close itself, but also when it tries to close report A. If this error isn't fatal then when B closes C, C's OnClose event starts. Assuming there are no further reports, C will try to close A (fails), B (fails) and will skip trying to close itself. It then closes for real, and control returns to B's OnClose. This has finished its loop (C being the last report) so it too terminates and the report B closes. Report A has only just started its loop, so it will still try to close B and C, but this will fail with a different error as the only report open will be A - so by this time, Reports(i) is invalid for any value of i > 0.

    The easiest solution might be to add error handling to CloseAllReports and in it, check first whether the error is either 2585 (This action can't be carried out while processing a form or report event.) or 2457 (The number you used to refer to the report is invalid). When it is, just resume processing, otherwise, report the error.

Posting Permissions

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