Cycling through controls

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
 
A

Allen Browne

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.
 
D

Duane Hookom

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.
 
L

Leslie Isaacs

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
 
L

Leslie Isaacs

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
 
A

Allen Browne

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
 

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