Close all open reports

I have this code that I have adapted from a post i found on here.
I want to close all open reports in the current database

The code is in the Timer event of a hidden form (frmRprtTimer) that opens when any report opens.

Option Compare Database
Option Explicit

Private Sub Form_Timer()
On Error GoTo Err_Form_Timer
Dim rpt As Report
Dim db As Database
Dim cont As Container
    Set db = CurrentDb()
    Set cont = db.Containers("Reports")
    For Each rpt In cont
        DoCmd.Close
    Next rpt
    DoCmd.Close acForm, "frmRprtTimer"
Exit_Form_Timer:
    Exit Sub
Err_Form_Timer:
    MsgBox Err.Description
    Resume Exit_Form_Timer
End Sub

I keep getting an error.
Can anyone help?
LVL 1
Stephen ByromWarehouse/ShippingAsked:
Who is Participating?
 
rockiroadsConnect With a Mentor Commented:
try this alternative


Private Sub Form_Timer()
    CloseAllReports
    DoCmd.Close acForm, "frmRprtTimer"
End Sub

Private Sub CloseAllReports()

    Dim i As Integer
    Dim sName As String
    
    For i = 0 To CurrentDb.Containers("Reports").Documents.count - 1
        sName = CurrentDb.Containers("Reports").Documents(i).Name
        Debug.Print "Closing", sName
        DoCmd.Close acReport, sName
    Next i
    
End Sub

Open in new window

0
 
rockiroadsCommented:
when you close the report via docmd.close you have to specify the type and report name (like your timer)
eg

docmd.close acreport, rpt.name
0
 
Stephen ByromWarehouse/ShippingAuthor Commented:
Tried that rocki.
I get
"Operation is not supported for this type of object"
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Stephen ByromWarehouse/ShippingAuthor Commented:
Your alternative code worked great.
Thanx for the help.
Much appreciated
0
 
rockiroadsCommented:
no worries :)
0
 
Stephen ByromWarehouse/ShippingAuthor Commented:
Getting answers and code from here has taught me a lot, and is much appreciated.
I have managed to build a decent Stock database with yours and other's help.
Is there a book or some other refernece that you would recommend that I could learn more about coding in VBA?
0
 
rockiroadsCommented:
I don't know of any books as my vba knowledge is all self taught. I did find one useful site for noobs though that you may find useful
http://functionx.com/vbaccess/index.htm
0
 
Stephen ByromWarehouse/ShippingAuthor Commented:
Thanx Rocki.
Very usefull site.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.