highlight an option label

oxicottin

Learning by pecking away....
Local time
Today, 04:03
Joined
Jun 26, 2007
Messages
888
I have an option group on my form and when I click on an radio button in the option group I want its label to change yellow. How can I do this?

Thanks
 
easiest with a naming convention, so you "know" the label name

labelxx.backcolor = vbred (say)

its hard to find the name of an attached label directly.
 
and you work with the option group (frame) click event not the label.
Code:
Private Sub Frame0_Click()
    Dim lngYellow As Long
    lngYellow = RGB(255, 255, 0)
    Me.Label3.ForeColor = lngYellow
End Sub
 
This code is working but every radio I select changes the label to yellow and doesnt change the previously selected radio back to white. Thanks!
Code:
Private Sub optNatureAndExtentGroup_AfterUpdate()

    If Me.optNatureandExtentGroup = 1 Then
    Me.lblHeadNeck.BackColor = vbYellow
   
ElseIf Me.optNatureandExtentGroup = 2 Then
    Me.lblRightShoulder.BackColor = vbYellow

ElseIf Me.optNatureandExtentGroup = 3 Then
    Me.lblLeftShoulder.BackColor = vbYellow
    
ElseIf Me.optNatureandExtentGroup = 4 Then
    Me.lblChest.BackColor = vbYellow
    
End If


End Sub
 
you'll have to add code to each option changing the others back to white. :)
 
would it be something like.
Code:
If Me.optNatureandExtentGroup = 1 Then
    Me.lblHeadNeck.BackColor = vbYellow
else
    Me.lblHeadNeck.BackColor = vbwhite
 
Code:
with me
    select case .optNatureandExtentGroup.value
        case 1   [COLOR="Green"]'selected button has a value of 1[/COLOR]
            .lblHeadNeck.BackColor = vbYellow
            .lblRightShoulder.BackColor = vbWhite
            .lblLeftShoulder.BackColor = vbWhite
            .lblChest.BackColor = vbWhite
        case 2
            .lblRightShoulder.BackColor = vbYellow
            [COLOR="Green"]'other ones are white[/COLOR]
        case 3
            .lblLeftShoulder.BackColor = vbYellow
            ...sim...
        case 4
            .lblChest.BackColor = vbYellow
            ...sim...
    end select
end with
 
Oh ok, I have 16 cases though :eek: Would this be the best way? Thanks!
 
with respect

the best way would be not to bother changing the colour!

its obvious which radio button is selected
 
not really, I have the radio butons over a muscular body that shows the locations of injured areas. Thanks!
 
you can't in vba immediately locate a label thats associated with a control, but you can find a control thats associated with a label

so, if you have an naming convention

(say the chkbox is chkbox1, and the labelis label1 then its easy to locate label

me.controls("label1")

or me.controls("label" & var_label) will be even more useful.

----------
but if you dont have a naming convention then you have to do a for each control test, until you find the control thats matched to the chkbox

--------------
instead of radio button group, why not consider using transparent buttons

you can overlay a tranparent button over an area of your form, and do what you want in respect to clicking the button
 
Im thinking the transparent button might be the way to go. One question about the radio butons, In an group if a radio button is ticked then how do you clear the option group if you dont want one ticked? Is there a setting that If you tick a radio button you can just click on it to unselect it? Thanks!
 
The following procedure, when placed into the OnClick event of a Frame control will change both the Fore-ground and back-ground colors of a radio buttons' label contained within that Frame.

First...the procedure changes the Back-Style for child Labels to all Radio Buttons to Transparent and sets the Fore Color for all labels to Black. (think of it as a reset). Then the Label attached to the selected Radio Button will have its BackStyle property changed to Solid, the Back-Ground colot to Yellow, and the Fore Color (text color) to Red.

This sample code assumes four Radio Buttons within a single Frame control. The child Labels for each Radio Button contain the same name except they also contain a number at the end of each name which corresponds with the Option Value for each Label Parent (Radio Button):

Code:
Private Sub Frame1_Click()
   Dim Ctrl As Control, i As Integer, v As Integer
   
   Set Ctrl = Me.Frame1
   v = Ctrl.Value
   
   For i = 1 To Ctrl.Controls.Count - 1
       If Ctrl.Controls(i).ControlType = acLabel Then
          Ctrl.Controls(i).ForeColor = vbBlack
          Ctrl.Controls(i).BackStyle = 0   [COLOR="DarkGreen"]'Transparent[/COLOR]
       End If
   Next i
   
   Me.Controls("OLabel" & v).ForeColor = vbRed
   Me.Controls("OLabel" & v).BackColor = vbYellow
   Me.Controls("OLabel" & v).BackStyle = 1  [COLOR="DarkGreen"]'Solid[/COLOR]

   Set Ctrl = Nothing
End Sub

This could have be done in other ways but here the For/Next loop shows you that you can enumerate through the Controls of a Frame control very much the same as you would enumerate through the Controls of a Form. Consider the Frame control as a Form so to speak.

.
 
I forgot to mention....to clear the selections within a Frame Control:

Me.MyFrameControlName.Value = 0

.
 
you can't in vba immediately locate a label thats associated with a control...
this gives you the label of a control:

?forms!frmYourForm!txtYourTextbox.controls(0).Name

or .caption etc.
 
so you can

i have got this the wrong way round then - if you have a label, are you unable to identify the attached textbox etc directly from the label
 
not sure if you're asking or not, but

?forms!frmYourForm!lblYourlabel.parent.name

gives you the associated control's name.
 
thanks wazz

i use the access cookbook for a lot of guidance

it says P68

"because access provides no way for linking a textbox to its attached label, there is no way of referring to a control's label"

i assumed this was correct, and just took this as read - obviously its not correct, as you just pointed out.

This will be really useful

Thanks
 
actually you can thank datAdrenaline for that one! i just figured out the .parent part afterward.
 
CyberLynx, Im not quit sure I know how your code works? I have 16 labels how would I inclue them into your code? Thanks!
 

Users who are viewing this thread

Back
Top Bottom