Referring to a control's associated label

  • Thread starter CrazyAccessProgrammer
  • Start date
C

CrazyAccessProgrammer

Is there a way in VBA to refer to a control's associated label without having
to know the name of the label?

for example say I have a text box named, 'MyTextBox' and it has an
associated label named, 'Label1'... is there a way I can manipulate
properties of Label1 when I only know the name of the textbox control named
MyTextBox?
 
D

Dirk Goldgar

CrazyAccessProgrammer said:
Is there a way in VBA to refer to a control's associated label without
having
to know the name of the label?

for example say I have a text box named, 'MyTextBox' and it has an
associated label named, 'Label1'... is there a way I can manipulate
properties of Label1 when I only know the name of the textbox control
named
MyTextBox?


Yes, the label will be in the control's Controls collection. You have to be
careful in the case of option group frames, which have multiple controls in
their Controls collections. But a text box has at most one:

With Me.txtMyTextbox

If .Controls.Count > 0 Then
.Controls(0).Caption = "New label caption"
End If

End With
 
D

Douglas J. Steele

If there's an associated label, it will be in the first item in the
control's Controls collection

Forms!SomeForm!MyTextBox.Controls(0).Caption = "New Caption"
 
S

Sietske

In addition: Is there a similar way in VBA to hide a control's associated
label? I'd like to hide all labels which are connected to a checkbox. I've
tried using "acLabel", but I couldn't find out how to keep the other labels
(connected to text boxes etc.) visible, except for making them visible again
one by one.

The checkboxes are already hidden using:

Dim MyCheckbox As Control

For Each MyCheckbox In Me.Section(acDetail).Controls
Select Case MyCheckbox.ControlType
Case acCheckBox
MyCheckbox.Visible = False
End Select
Next

By the way: I've named all labels connected to checkboxes "chklbl......".
Maybe that could be of any help?
 
D

Dirk Goldgar

Sietske said:
In addition: Is there a similar way in VBA to hide a control's associated
label? I'd like to hide all labels which are connected to a checkbox. I've
tried using "acLabel", but I couldn't find out how to keep the other
labels
(connected to text boxes etc.) visible, except for making them visible
again
one by one.

The checkboxes are already hidden using:

Dim MyCheckbox As Control

For Each MyCheckbox In Me.Section(acDetail).Controls
Select Case MyCheckbox.ControlType
Case acCheckBox
MyCheckbox.Visible = False
End Select
Next

By the way: I've named all labels connected to checkboxes "chklbl......".
Maybe that could be of any help?


Normally, if a control has an attached label, hiding the control will make
the label invisibile, too. Are you saying that's not working for you?
 
S

Sietske

Yes, this is indeed not working for me.

In fact, the "for each"-loop is called from a subform to its parent form.
Could that be the reason that it does not work correctly? My code is

For Each MyCheckbox In Me.Parent.Section(acDetail).Controls
Select Case MyCheckbox.ControlType
Case acCheckBox
MyCheckbox.Visible = False
End Select
Next

I left out the Parent-part in the previous post, because at that time I
thought that it wouldn't be of much influence on the answer.
 
D

Dirk Goldgar

Sietske said:
Yes, this is indeed not working for me.

In fact, the "for each"-loop is called from a subform to its parent form.
Could that be the reason that it does not work correctly? My code is

For Each MyCheckbox In Me.Parent.Section(acDetail).Controls
Select Case MyCheckbox.ControlType
Case acCheckBox
MyCheckbox.Visible = False
End Select
Next

I left out the Parent-part in the previous post, because at that time I
thought that it wouldn't be of much influence on the answer.


I think you're probably right that it doesn't matter; at least, I can't see
why it should. Are you sure that the labels you are talking about are, in
fact, attached to the check boxes, not merely positioned beside them? If
selecting and moving the check box (in design view) doesn't also move the
label, then the label isn't attached to the check box.

If it turns out that the labels aren't attached to the check boxes, then
there are a couple of ways I could think of to code around it. However, the
easiest way to deal with it would be to reattach the labels to the check
boxes.
 

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