Cycling through controls

  • Thread starter Thread starter Leslie Isaacs
  • Start date Start date
L

Leslie Isaacs

Hello All

What wrong with this:

Private Sub Report_Open(Cancel As Integer)
Dim ctl As Control
For Each ctl In Me.Controls
If Me.Caption = "W Ratcliffe" Then
Me.Caption = "H Manley"
Else
'do nothing
End If
Next
End Sub

Many thanks
Leslie Isaacs
 
Me.Caption sets the Title bar for the report itself.

Not sure why it's necessary to loop through all controls to set the report's
title bar.
 
I expect you need:
Private Sub Report_Open(Cancel As Integer)
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Caption = "W Ratcliffe" Then
ctl.Caption = "H Manley"
Else
'do nothing
End If
Next
End Sub
I would also add code to handle errors for controls that don't have
captions.
 
Hello Allen

Thanks for your reply: my mistake - I want to set each label's caption, not
the report's title bar. Using Duane's code works fine - except that I need
to capture the error where a control does not have a caption.

Thanks again
Les
 
Hello Duane

Many thanks for your reply.

I now have:

Private Sub Report_Open(Cancel As Integer)
Dim ctl As Control

For Each ctl In Me.Controls

If ctl.ControlType = acLabel Then
If ctl.Caption = "W Ratcliffe" Then
ctl.Caption = "H Manley"
Else
'do nothing
End If

Else
'do nothing
End If
Next
End Sub

.... which works great!
Thanks again
Les
 
Add error handling to the routine.
What error do you get?

If 438, the error handler would include:

If Err.Number = 438 Then
Resume Next
Else
MsgBox Err.Number & " - " & Err.Description
End If

If you are not sure how to add an error handler, see:
http://allenbrowne.com/ser-23a.html
 
Back
Top