By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,400 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,400 IT Pros & Developers. It's quick & easy.

accessing label linked to a control

P: n/a
Rab
hi

i need to programmatically change the caption of a label that is linked
to another control (i.e. textbox, combobox). it's for a generic
function where i only know the textbox/combobox name but not the name
of the label that is linked to the textbox/combobox.

example sub:

public sub ReSetCaption()
dim ctl as Control
for each ctl in Me.Controls
if Typeof ctl is TextBox or TypeOf ctl is ComboBox then
'
'write code to set the caption of the linked label to ""
'
end if
next
End Sub

is there anyway this can be accomplished?

TIA

Rab

Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Rab" <sa***************@arvatoservices.co.uk> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
hi

i need to programmatically change the caption of a label that is linked
to another control (i.e. textbox, combobox). it's for a generic
function where i only know the textbox/combobox name but not the name
of the label that is linked to the textbox/combobox.

example sub:

public sub ReSetCaption()
dim ctl as Control
for each ctl in Me.Controls
if Typeof ctl is TextBox or TypeOf ctl is ComboBox then
'
'write code to set the caption of the linked label to ""
'
end if
next
End Sub

is there anyway this can be accomplished?

TIA

Rab


I would loop through all the controls and if the control is a label then see
what its parent is - i.e. what it's attached to. If you have an un-attached
label then its parent will be the form. The sub below is an example, which
could be called within any form with: SetLabelCaptions Me

Public Sub SetLabelCaptions(MyForm As Form)

On Error GoTo Err_Handler

Dim ctl As Control

For Each ctl In MyForm.Controls

With ctl

If .ControlType = acLabel Then

If .Parent.Name <> MyForm.Name Then

Select Case .Parent.ControlType

Case acTextBox
.Caption = "TB~" & .Parent.ControlSource

Case acComboBox
.Caption = "CB~" & .Parent.ControlSource

Case acCheckBox
.Caption = "CH" & .Parent.ControlSource

End Select

End If

End If

End With

Next ctl

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Nov 13 '05 #2

P: n/a
Rab
thanks for the solution, justin. however, is there anyway i can access
the child control (a label in this case) through the parent instead of
vice versa?

Nov 13 '05 #3

P: n/a
"Rab" <sa***************@arvatoservices.co.uk> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
thanks for the solution, justin. however, is there anyway i can access
the child control (a label in this case) through the parent instead of
vice versa?

I can't think of one. Is there something the suggested approach won't do?
I would have thought it would suit your needs perfectly.
Nov 13 '05 #4

P: n/a
Rab
it will take care of this function, but in another function, all i want
to pass is the name of a textbox control and based on its contents, the
associated label caption needs to be changed.

Nov 13 '05 #5

P: n/a
Rab wrote:
hi

i need to programmatically change the caption of a label that is
linked to another control (i.e. textbox, combobox). it's for a generic
function where i only know the textbox/combobox name but not the name
of the label that is linked to the textbox/combobox.


An attached label is always the first item in the Controls collection of the
parent control.

Me.TextBoxName.Controls(0)

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #6

P: n/a

"Rab" <sa***************@arvatoservices.co.uk> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
it will take care of this function, but in another function, all i want
to pass is the name of a textbox control and based on its contents, the
associated label caption needs to be changed.


For a single textbox, you could have the sub below. Note the error handling
as not all texboxes will have attached labels.

Public Sub SetCaption(txtBox As TextBox, strCaption As String)

On Error GoTo Err_Handler

txtBox.Controls(0).Caption = strCaption

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub
Nov 13 '05 #7

P: n/a
Rab
u guys are amazing!

thanks

Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.