Delete controls from a report

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

I have the following code in a module. I am trying to
delete controls on a report in design mode that are needed
when a particular report needs to be printed. I make a
copy of the report and then open the report in design
mode. I would like to delete all of the elements (text
boxes, labels) in the report that begin with names
like "xxx". I have cutoff the 1st 4 characters of each
element. When I run the code, it only deletes 3 out of
about 10 in needs to delete. What's going on.

here is a the code.

DoCmd.DeleteObject acReport, "zztest"
DoCmd.CopyObject , "zzTest",
acReport, "rptStats_PatientsServed_Join"

DoCmd.OpenReport "zztest", acViewDesign

Dim rpt As Report
Dim ctl As Control
Set rpt = Screen.ActiveReport
Dim thing As String
Set ctl = Nothing

For Each ctl In rpt.Controls
Debug.Print "CONTROL NAME: "; ctl.name
thing = Mid(ctl.name, 1, 4)
Debug.Print "THING: "; thing
If thing = "grp3" Then
DeleteReportControl "zztest", ctl.name
End If
Next

Set ctl = Nothing
 
I can't see why you are deleting the controls in the first place. Why not just
set there visible property to false in the open or load event of the form. It
will be faster and you won't be running into the bloat that will develop with
deleting and adding an object over and over.

That said, the reason you are only getting some of the controls is that when you
delete a control all the following controls get moved up in the que.

Delete control 4
Control 5 becomes 4
Control 6 becomes 5

So, now you've skipped what was control 5.

Try setting a counter and looping through the controls from the highest to the lowest.

For iCount = iControlCounts to 1 Step -1
Ctl = rpt.controls(icount)

With ctl
If Left(.Name,4) = "Grp3" then
...
End if

End with

Next iCount
 
Thank you John.
I tried your solution and it worked. The reason why I was
doing this was because--and maybe you would have a better
approach. I am giving the user the ability to pick 5
different grouping levels. The user can pick 2 or all 5.
I have a report template that has all 5 levels defined
with generic grouping labels. If the user chooses 2
levels, I dynamically create the query that creates the
generic grouping levels. The report is tied to this. I
could hide the other grouping levels but since there is
elements there they will not shrink thus I have large gaps
in the report. I thought I would copy off the template,
and delete the groups not used in the copy. I thought this
was easier than creating the grouping levels since there
are many elements within the groups and I didn't want to
have to worry about positioning. Thanks a bunch for your
help
-----Original Message-----
I can't see why you are deleting the controls in the first place. Why not just
set there visible property to false in the open or load event of the form. It
will be faster and you won't be running into the bloat that will develop with
deleting and adding an object over and over.

That said, the reason you are only getting some of the controls is that when you
delete a control all the following controls get moved up in the que.

Delete control 4
Control 5 becomes 4
Control 6 becomes 5

So, now you've skipped what was control 5.

Try setting a counter and looping through the controls
from the highest to the lowest.
 
UHM, if you set the visible property of a section to False, then it doesn't take
any room when it prints (because it doesn't print).
 
I can't see why you are deleting the controls in the first place. Why not just
set there visible property to false in the open or load event of the form. It
will be faster and you won't be running into the bloat that will develop with
deleting and adding an object over and over.

That said, the reason you are only getting some of the controls is that when you
delete a control all the following controls get moved up in the que.

Delete control 4
Control 5 becomes 4
Control 6 becomes 5

So, now you've skipped what was control 5.

Try setting a counter and looping through the controls from the highest to the lowest.

For iCount = iControlCounts to 1 Step -1
Ctl = rpt.controls(icount)

With ctl
If Left(.Name,4) = "Grp3" then
...
End if

End with

Next iCount
Hi John Spenser. I was trying to resolve the same problem yesterday and thought of using a counter this morning. Really spot on. Thanks.
 
Back
Top