Loop through Report Controls collection

A

Alan Z. Scharf

I want to loop through the collection of controls on a report so that I can
set the font size of each depending on the [Pages] count in a Report Header
OnPrint event.

My code below produces "Object doesn't support this method"

Can someone help with proper syntax please?

Thanks.

Alan

Code
------
Private Sub PageHeader0_Print(Cancel As Integer, PrintCount As Integer)
' Reduce font size to 10 from present 11 for all fields if page count >
2

If [Pages] > 2 Then
Dim col As New Collection
Dim ctl As Control

For Each ctl In Me.Controls
Me.Controls(ctl).FontSize = 18
Next
End If
End Sub
 
T

Tim Ferguson

My code below produces "Object doesn't support this method"

You don't say which line causes the error, so I can only hazard a guess
that it is this one:
Me.Controls(ctl).FontSize = 18

Not all controls have a .FontSize property. Easiest thing is simply to dump
the error by wrapping it in a On Error Resume Next... On Error Goto 0 pair;
or else you could test explicitly by iterating the ctl.Properties
collection.

Incidentally, I have just noticed that the line as quoted is not even
legal. You can index the .Controls collection using a number or a string
(i.e Me.Controls(12) or Me.Controls("sfrmVisits") ) but ctl is dimmed as a
Control. I assume this is a typo and you meant

ctl.FontSize = 18

HTH


Tim F
 
A

Alan Z. Scharf

Tim,

Thanks for your reply.

I got it using:

For Each ctl In Controls
ctlName = ctl.Name
If Me.Controls(ctlName).ControlType = acLabel Or
Me.Controls(ctlName).ControlType = acTextBox Then
Me.Controls(ctlName).FontSize = 10
End If
Next
 
D

Douglas J. Steele

Your code is doing a lot more work than is required:

For Each ctl In Controls
If ctl.ControlType = acLabel Or ctl.ControlType = acTextBox Then
ctl.FontSize = 10
End If
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top