Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing all open reports from the OnActivate event of a form 2

Status
Not open for further replies.

ernigles

Technical User
Jan 15, 2009
31
ES
Hi, i want to close each report of a DB when it loose the focus, and since the forms that gain that focus can only be two, it's less hard to handle it from these two forms, on the event OnActivate of the forms.

But, how can i know which report is which is open in each moment, for closing that specific report and no other?

Thanks for any help given.
 
for a = 0 to reports.counts

docmd.close acReport ,reports(a).name,2

next
 
Thanks pwise.

I see the code that works is

Code:
If Reports.Count > 0 Then
For a = 0 To Reports.Count - 1
 DoCmd.Close acReport, Reports(a).Name, 2
Next a

I don't know if the last line could be also

Code:
Next



But the problem is in which event of the main form i put that code: in the OnActivate event, this event occurs in some moment between Docmd.Openreport and the report is really shown, so the report doesn't achieve appearing. I have also tried in the event GotFocus but this event doesn't enter.
 
I'd go for the On Deactivate event of the report.

DoCmd.Close acReport, Me.Name

And you're right about the code. When you remove items from a collection, one working method is to loop "from the end".

Roy-Vidar
 
Thanks RoyVidar.

I have do what you suggest and i have seen that in some moment before showing the report, its OnDeactivate event enters, so it closes before being shown.
 
I haven't seen that. Do you have some code running after you've opened the report that either changes focus, or causes focus to change?

Roy-Vidar
 
RoyVidar, the code is run from a printing popup form when still is open another main form that can catch the focus in some moment of the process.

The code is:

Code:
    Set db = CurrentDb()
    Set qD = db.CreateQueryDef("", "Delete * From [tblIndiceInformeGeneral]")
    qD.Execute
    qD.Close
    Set qD = db.CreateQueryDef("", "Delete * From [tblIndiceInformeGrSgrCult1]")
    qD.Execute
    qD.Close

    DoCmd.OutputTo acOutputReport, "infGeneralParcial_paginador", acFormatSNP, CurrentProject.Path & "\temp1.snp", False
    DeleteFile ("" & CurrentProject.Path & "\temp1.snp")
    DoCmd.OutputTo acOutputReport, "infGrSgrCultivoSel_paginador", acFormatSNP, CurrentProject.Path & "\temp2.snp", False
    DeleteFile ("" & CurrentProject.Path & "\temp2.snp")

    Set db = CurrentDb
    Set rst = db.openrecordset("tblIndiceInformeGeneral")
    Do Until rst.EOF = True
        With rst
            .Edit
            rst!pagina = rst!pagina + IncrementNPagesPG
            .Update
            .MoveNext
        End With
    Loop
    Set rst = Nothing
    Set db = Nothing
    
    NPageRef = NPageRef + IncrementNPagesPG

    DoCmd.OutputTo acOutputReport, "infGeneralConPGINDICE_paginador", acFormatSNP, CurrentProject.Path & "\temp3.snp", False
    DeleteFile ("" & CurrentProject.Path & "\temp3.snp")

    DoCmd.OpenReport "infGeneralParcialConPG", acViewPreview
    DoCmd.Close acForm, "frmImprimir"
    DoCmd.SelectObject acReport, "infGeneralParcialConPG"
    DoCmd.Maximize
    If GetVersion() <> "12.0" Then
        DoCmd.ShowToolbar "Ventana2", acToolbarYes
        DoCmd.ShowToolbar "Print Preview", acToolbarNo
    Else
        DoCmd.ShowToolbar "Ribbon", acToolbarYes
        DoCmd.ShowToolbar "Print Preview", acToolbarYes
    End If


It shows the print preview of the report 'infGeneralParcialConPG' from the form frmImprimir, and before it does some DoCmd.OutputTo and after DeleteFile in order to include an index.
 
You do

[tt] DoCmd.Close acForm, "frmImprimir"[/tt]

when the report is open. I'm guessing that removes focus from the report, firing the On Deactivate of the report, thus closing the report. But it could be other code, too.

In stead one might consider just making it invisible

[tt]if currentproject.allforms("frmImprimir").isloaded then
forms("frmImprimir").visible = false
end if[/tt]

then close it in an appropriate event, for instance the on deactivate of the report, or the on close of the report?

[tt]if currentproject.allforms("frmImprimir").isloaded then
docmd.close acform, "frmImprimir"
end if[/tt]


Roy-Vidar
 
Thank you, Roy-Vidar. It suits what i need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top