code closing multiple reports

  • Thread starter Thread starter Sarah
  • Start date Start date
S

Sarah

I have VBA where I need to close any reports that are currently open. All of
the reports in question have names starting with "rptGroup" followed by 2
more characters. There are up to 17 of these reports that could be open, so
I use this coding to be sure I catch any open ones;

DoCmd.Close acReport, "rptGroup01"
DoCmd.Close acReport, "rptGroup02"
DoCmd.Close acReport, "rptGroup03"
.......
DoCmd.Close acReport, "rptGroup17"

is there a way to more simply close all reports LIKE "rptGroup*"?

thanks in adavance
Sarah
 
Loop backwards (since you're reducing the count) through the Reports
collection:

Dim i As Integer
For i = Reports.Count -1 To 0
If Reports(i).Name Like "rptGroup*" Then
DoCmd.Close acReport, Reports(i).Name
End If
Next
 
Dim rpt As Report
For Each rpt In Reports
If Left(rpt.Name) = "rptGroup" Then
DoCmd.Close acReport, rpt.Name
End If
Next rpt


hth
--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
And just for future reference, the Reports collection contains all *open*
reports... the AllReports collection contains all the reports in the db, open
or not. Same goes for Forms.

Might come in handy ;-)

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
Well my first thought was "nope, not a fade... it's always been dim like that"

But on a serious note, did I miss something? I'm not seeing anything wrong
there...
 
Jack Leach said:
But on a serious note, did I miss something? I'm not seeing anything
wrong
there...

The Left function requires a second argument to indicate how much of the
string to examine.
 
Jack Leach said:
I'm going to go hide in the corner now...

Lol. While you're there , remember that if you fall flat on your face you're
still moving forward.

I think it was the period in rpt.Name wot dun it. Your brain probably saw it
as a comma. Tricky things, brains. The following is a bit off-topic but it
illustrates the scenario perfectly:

Can you raed this? Olny 55 plepoe out of 100 can.

I cdnuolt blveiee that I cluod aulaclty uesdnatnrd what I was rdanieg.

The phaonmneal pweor of the hmuan mnid, aoccdrnig to a rscheearch at
Cmabrigde Uinervtisy, it dseno't mtaetr in what oerdr the ltteres in a word
are, the olny iproamtnt tihng is that the frsit and last ltteer be in the
rghit pclae.

The rset can be a taotl mses and you can still raed it whotuit a pboerlm.

This is bcuseae the huamn brian deos not raed ervey lteter by istlef, but
the
word as a wlohe.

Azanmig huh? Yaeh and I awlyas tghuhot slpeling was ipmorantt!
 
hi Allen: This worked great, but first I had to make 2 changes:
a) I had to check that Reports.Count > 0 (because sometimes none of the
reports are open when the code is run, so I end up considering Reports(-1) in
the 'IF' statement).
b) I had to add 'STEP -1' to the 'FOR' statement.

Sarah
 
Back
Top