Referring to labels on a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having trouble with referencing labels on a form. I need to be able to
address about 120 labels on a form On_Activate and change the caption of each
based on a user's preference stored in a table. The table is:
UserName (Each user has specified templated text that goes into the text
box)
Button Name (refering to the label name on the form)
Button Caption (The new caption to replace the existing caption)
Text (Text to be inserted into the label's associated text box when
clicked by user)

I have attempted to do this in this way (Debug.print used to test the code
to see what works and what doesn't):

Do Until rst.EOF
Debug.Print "rstBN="; rst.Fields("ButtonName") 'This works fine...
BN = rst.Fields("ButtonName")
Debug.Print "BN="; BN 'This works fine also...
Debug.Print Me.BN 'This doesn't work...
Debug.Print Me.BN.Caption 'Nor does this...
BC = rst.Fields("ButtonCaption") 'Nor this...
Debug.Print "rstBC="; rst.Fields("ButtonCaption") 'Nor this...
'rst.MoveNext
'Loop

I am lost as to what to try next. Any help would be appreciated.

Can anyone help me?
 
I am having trouble with referencing labels on a form. I need to be able to
address about 120 labels on a form On_Activate and change the caption of each
based on a user's preference stored in a table. The table is:
UserName (Each user has specified templated text that goes into the text
box)
Button Name (refering to the label name on the form)
Button Caption (The new caption to replace the existing caption)
Text (Text to be inserted into the label's associated text box when
clicked by user)

I have attempted to do this in this way (Debug.print used to test the code
to see what works and what doesn't):

Do Until rst.EOF
Debug.Print "rstBN="; rst.Fields("ButtonName") 'This works fine...
BN = rst.Fields("ButtonName")
Debug.Print "BN="; BN 'This works fine also...
Debug.Print Me.BN 'This doesn't work...
Debug.Print Me.BN.Caption 'Nor does this...
BC = rst.Fields("ButtonCaption") 'Nor this...
Debug.Print "rstBC="; rst.Fields("ButtonCaption") 'Nor this...
'rst.MoveNext
'Loop

I am lost as to what to try next. Any help would be appreciated.

It sounds like you may be getting confused between Fields (which exist
in tables) and Controls (which are objects on a Form, such as Labels).

Try

Me.Controls(BN).Caption = rstBN![Button Caption]

The Label and the Textbox will both be controls on the form, but they
will (necessarily) have different names. I'm not sure if setting a
textbox's Caption property changes the caption of the associated label
or not!

<testing> Nope. A textbox LicenseOwnerID has an associated label
Label76, and attempting to change the textbox's Caption property gets
an error "does not support this property".

You may need to come up with a naming convention, such as naming the
textbox XYZ txtXYZ and its associated label lblXYZ.

John W. Vinson[MVP]
 
Thank you, John. I tried it (reassigning text box names and making label
names reflect the text box names) but I am still unable to address the
Caption property. I appreciate your efforts. I'll keep trying different
things.

John Vinson said:
I am having trouble with referencing labels on a form. I need to be able to
address about 120 labels on a form On_Activate and change the caption of each
based on a user's preference stored in a table. The table is:
UserName (Each user has specified templated text that goes into the text
box)
Button Name (refering to the label name on the form)
Button Caption (The new caption to replace the existing caption)
Text (Text to be inserted into the label's associated text box when
clicked by user)

I have attempted to do this in this way (Debug.print used to test the code
to see what works and what doesn't):

Do Until rst.EOF
Debug.Print "rstBN="; rst.Fields("ButtonName") 'This works fine...
BN = rst.Fields("ButtonName")
Debug.Print "BN="; BN 'This works fine also...
Debug.Print Me.BN 'This doesn't work...
Debug.Print Me.BN.Caption 'Nor does this...
BC = rst.Fields("ButtonCaption") 'Nor this...
Debug.Print "rstBC="; rst.Fields("ButtonCaption") 'Nor this...
'rst.MoveNext
'Loop

I am lost as to what to try next. Any help would be appreciated.

It sounds like you may be getting confused between Fields (which exist
in tables) and Controls (which are objects on a Form, such as Labels).

Try

Me.Controls(BN).Caption = rstBN![Button Caption]

The Label and the Textbox will both be controls on the form, but they
will (necessarily) have different names. I'm not sure if setting a
textbox's Caption property changes the caption of the associated label
or not!

<testing> Nope. A textbox LicenseOwnerID has an associated label
Label76, and attempting to change the textbox's Caption property gets
an error "does not support this property".

You may need to come up with a naming convention, such as naming the
textbox XYZ txtXYZ and its associated label lblXYZ.

John W. Vinson[MVP]
 
Back
Top