Highlight option label

S

SoggyCashew

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?
 
F

fredg

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?

Here is an example for a 4 button OptionGroup.
Code the Option Group's AfterUpdate event:

If Me.Frame277 = 1 Then
Me.Label281.BackColor = vbYellow
Me.Label283.BackColor = vbWhite
Me.Label285.BackColor = vbWhite
Me.Label290.BackColor = vbWhite

ElseIf Me.Frame277 = 2 Then
Me.Label281.BackColor = vbWhite
Me.Label283.BackColor = vbYellow
Me.Label285.BackColor = vbWhite
Me.Label290.BackColor = vbWhite

ElseIf Me.Frame277 = 3 Then
Me.Label281.BackColor = vbWhite
Me.Label283.BackColor = vbWhite
Me.Label285.BackColor = vbYellow
Me.Label290.BackColor = vbWhite

ElseIf Me.Frame277 = 4 Then
Me.Label281.BackColor = vbWhite
Me.Label283.BackColor = vbWhite
Me.Label285.BackColor = vbWhite
Me.Label290.BackColor = vbYellow
End If

Change the OptionGroup's name (Frame277) as well as the name of the
labels within the group as needed. Make sure each label's BackStyle
property is set to Normal.
 
S

SoggyCashew

Fred that works but if I select another or a different choice in the option
group then the new label is yellow but the old one stays yellow as well
insted of going back to white. Here is what I have...

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
 
T

tina

add the following function procedure to a standard module, as

Public Function isOptionBackColor(Optional ByVal str As String)

Dim ctlObj As Control, lng As Long
Const lngSelected As Long = 12713983 ' light yellow
Const lngNotSelected As Long = 16777215 ' white

With Screen.ActiveControl
For Each ctlObj In .Controls
If TypeOf ctlObj Is Label And Not (ctlObj.Name = str) Then
If ctlObj.Parent.OptionValue = .Value Then
ctlObj.BackColor = lngSelected
Else
ctlObj.BackColor = lngNotSelected
End If
End If
Next
End With

End Function

then call it from the option group control's AfterUpdate property line. to
do that, open the form in Design view and click once on the option group
control to select it. in the Properties box, click on the Event tab and add
the following code to the event line, as

=isOptionBackColor()

if your option group control has a label (i'm talking about the option group
itself, not the option boxes inside of it), then click on that label to
select it, and look at the Name property in the Properties box. click back
to the option group control and add that name to the function call, as

=isOptionBackColor("PutLabelNameHereBetweenDoubleQuotes")

this code has the advantage of working regardless of the number of options
in the option group control, and the only name you need is the name of the
option group control's label (if there is one). the code can be called from
the AferUpdate event property line of any option group control in any form
in the database.

hth
 
T

tina

oops, i noticed one error in the code. replace the line

Dim ctlObj As Control, lng As Long

with the following line, as

Dim ctlObj As Control

and btw, you can change the hard coded colors "light yellow" and "white" to
whatever colors you want to use. also, with a minor change to the function,
it can be set up to use different back colors for different option groups,
rather than the single set of colors available in the posted code.

hth


tina said:
add the following function procedure to a standard module, as

Public Function isOptionBackColor(Optional ByVal str As String)

Dim ctlObj As Control, lng As Long
Const lngSelected As Long = 12713983 ' light yellow
Const lngNotSelected As Long = 16777215 ' white

With Screen.ActiveControl
For Each ctlObj In .Controls
If TypeOf ctlObj Is Label And Not (ctlObj.Name = str) Then
If ctlObj.Parent.OptionValue = .Value Then
ctlObj.BackColor = lngSelected
Else
ctlObj.BackColor = lngNotSelected
End If
End If
Next
End With

End Function

then call it from the option group control's AfterUpdate property line. to
do that, open the form in Design view and click once on the option group
control to select it. in the Properties box, click on the Event tab and add
the following code to the event line, as

=isOptionBackColor()

if your option group control has a label (i'm talking about the option group
itself, not the option boxes inside of it), then click on that label to
select it, and look at the Name property in the Properties box. click back
to the option group control and add that name to the function call, as

=isOptionBackColor("PutLabelNameHereBetweenDoubleQuotes")

this code has the advantage of working regardless of the number of options
in the option group control, and the only name you need is the name of the
option group control's label (if there is one). the code can be called from
the AferUpdate event property line of any option group control in any form
in the database.

hth
 
F

fredg

Fred that works but if I select another or a different choice in the option
group then the new label is yellow but the old one stays yellow as well
insted of going back to white. Here is what I have...

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

Yes, my code works, but you did not follow my code.
Please follow ALL of my code, not just the lines you choose.
If you change one label to yellow you must change all of the other
labels in the option group to white.

If Me.optNatureandExtentGroup = 1 Then
Me.lblHeadNeck.BackColor = vbYellow
Me.lblRightShoulder.BackColor = vbWhite
Me.lblLeftShoulder.BackColor = vbWhite
Me.lblChest.BackColor = vbWhite
ElseIf .... etc.......

Try again.
 
S

SoggyCashew

Tina, that worked like a charm and with very little code. How do I get it to
use different back colors for different option groups? Thanks!
--
Thanks,
Chad


tina said:
oops, i noticed one error in the code. replace the line

Dim ctlObj As Control, lng As Long

with the following line, as

Dim ctlObj As Control

and btw, you can change the hard coded colors "light yellow" and "white" to
whatever colors you want to use. also, with a minor change to the function,
it can be set up to use different back colors for different option groups,
rather than the single set of colors available in the posted code.

hth
 
T

tina

here's a revised function that will do the trick:

Public Function isOptionBackColor(Optional ByVal str As String, _
Optional ByVal lngSelected As Long, _
Optional ByVal lngNotSelected As Long, _
Optional ByVal blnTransparent = False)

Dim ctlObj As Control
Const lngSel As Long = 12713983 ' light yellow
Const lngNotSel As Long = 16777215 ' white

If lngSelected = 0 Then lngSelected = lngSel
If lngNotSelected = 0 Then lngNotSelected = lngNotSel

With Screen.ActiveControl
For Each ctlObj In .Controls
If TypeOf ctlObj Is Label And Not (ctlObj.Name = str) Then
If ctlObj.Parent.OptionValue = .Value Then
ctlObj.BackColor = lngSelected
ctlObj.BackStyle = 1
Else
If blnTransparent Then ctlObj.BackStyle = 0
ctlObj.BackColor = lngNotSelected
End If
End If
Next
End With

End Function

you can set "default" backcolors, by changing the value of the constants
lngSel and lngNotSel. then you can override one, or both, default colors by
providing one or both of the optional arguments in the function call. the
caveat is this: if you call the function *directly from the Event line in
the Properties box*, you have to provide all three arguments, or none. for
instance, if you do not have an option group label, and you want to override
only the "not selected" default color, you have to enter a "dummy" label
name (or a zero-length string, as ""), and enter the default "selected"
color, as well as the override "not selected" color, as

=isOptionBackColor("x",12713983,14680031)

however, if you call the function from within the AfterUpdate event
procedure, you can skip unneeded arguments, as

Private Sub optNatureAndExtentGroup_AfterUpdate()

isOptionBackColor , , 14680031

End Sub

and for those folks who, like me, prefer to have their labels' BackStyle
transparent, i went whole-hog and added an optional argument for "not
selected" labels to be transparent to the form background, while "selected"
labels show either the default backcolor or replacement backcolor.

hth
 
T

tina

now, the problem with the code in my previous post is that, while it works
great in an option group control's AfterUpdate event, what happens when your
option group control is bound to a field in the form's RecordSource? in that
case, it's likely that you'll be looking at *existing* records from time to
time, and you'll want the correct option highlighted each time you move from
one record to another. for that to happen, you need to run code in the
*form's* Current event; but the previous code will not run in a form's
Current event, so it's no good for this scenario.

so let's change the function one more time, so it will run in both the
form's Current event, and also in the option group control's AfterUpdate
event, and regardless of whether the option group control is bound or
unbound, as

Public Function isOptionBackColor(ByVal ctlOptionGrp As OptionGroup, _
Optional ByVal str As String, Optional ByVal lngSelected As Long, _
Optional ByVal lngNotSelected As Long, _
Optional ByVal blnTransparent = False)

Dim ctlObj As Control
Const lngSel As Long = 12713983 ' light yellow
Const lngNotSel As Long = 16777215 ' white

If lngSelected = 0 Then lngSelected = lngSel
If lngNotSelected = 0 Then lngNotSelected = lngNotSel

With CodeContextObject
With ctlOptionGrp
For Each ctlObj In .Controls
If TypeOf ctlObj Is Label And Not (ctlObj.Name = str) Then
If ctlObj.Parent.OptionValue = .Value Then
ctlObj.BackColor = lngSelected
ctlObj.BackStyle = 1
Else
If blnTransparent Then ctlObj.BackStyle = 0
ctlObj.BackColor = lngNotSelected
End If
End If
Next
End With
End With

End Function

to run the above code, you must always include the first argument, but it's
just the control name. call it from the Event property line as

=isOptionBackColor([optNatureAndExtentGroup])

and remember, if you include one optional argument, you must include all of
them.
or call it from within the event procedure as

isOptionBackColor Me!optNatureAndExtentGroup

remembering again that within the VBA module, you can skip any or all
optional arguments.

hth
 

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