settings acOptionGroup

G

Guest

I have a form with 4 acOptionGroup. Each one has 4 options and the color of
each option is black.
Question:
For each option selected, I would like to change the color from black to
blue; how can I do this?

Thanks
jcp
 
A

Allen Browne

Use the AfterUpdate event procedure to set the BackColor or ForeColor of the
label attached to the option button.

Compare the Value of the option group to the OptionValue of the buttons
until you get a match. Once you have the option button, you can refer to its
attached label as Controls(0), e.g.:
Me.MyOptionButton.Controls(0).ForeColor = vbBlue

Don't forget to reset the color of the label attached to the other option
button that was just deselected.
 
G

Guest

Hi Allen,

I did a function and it works well. I know you are an expert, I will pass to
here what I did.
However, I can improve this function, if is possible to know the number of
options for each Group. Is it possible?

thanks
Here you are the code, is working

1. First I re-name the labels for each OptionGroup with following criteria
Example, Option Group with 3 options
OptionGroup Name: Test
Label Name for option1: TestLB1 '1 is the value that the option receive
Label Name for option2: TestLB2
Label Name for option3: TestLB3

2. I created the function in the form, off course is the function is out the
form is need to add byref.

Function ChangeColorOptionGroup(grpName As String, k As Integer)
'grpName OptionGroup name
'k Number of Options

On Error GoTo ErrTrack

Dim lb As String 'OptionGroup Label Name
Dim zC As Long 'Default Color
Dim xC As Long 'Change Color
Dim zW As Long 'default FontWeight 300 (normal)
Dim xW As Long 'change FontWeight =600(semi-bold)

zC = 0 'Black
xC = 130 'Brown
zW = 300 'Normal
xW = 600 'Semi-bold
lb = grpName & "LB"

For i = 1 To k
If Me(grpName).Value = i Then
With Me(lb & i)
.ForeColor = xC
.FontWeight = 600
End With
Else
With Me(lb & i)
.ForeColor = zC
.FontWeight = 300
End With
End If
Next i

Exit_ErrTrack:
Exit Function
ErrTrack:
If Err.Number = 2465 Then
Resume Next
Else
End If

'MsgBox Err.Number & vbLf & Err.Description, , iMsg
Resume Exit_ErrTrack

End Function

3. I can applly this function any OptionGroup, giving the name and the
number of options.
For each group I call the function in AfterUpdate event, as:
Call ChangeColorOptionGroup("Test", 3)

Thanks
 
G

Guest

Allen, thank your for your suggestions

Now I could create a geral function, I don't need re-name the options
label.name and it works in all forms that I need change color.

One quesntion that I didn´t understand. what means (0) in ctl.Controls(0).
I ssw index???

here, I leave the function if somebody needs, it works, :)


Function ChangeColorOptionGroup(ByRef frm As Form, grp As OptionGroup)
On Error GoTo ErrTrack

Dim ctl As Control
'Settings
Dim zC As Long 'Default Color
Dim xC As Long 'Change Color
Dim zW As Long 'default FontWeight 300 (normal)
Dim xW As Long 'change FontWeight =600(semi-bold)

zC = 0 'Black
xC = 130 'Brown
zW = 300 'Normal
xW = 600 'Semi-bold

For Each ctl In grp.Controls
If ctl.ControlType = acOptionButton Then
If ctl.OptionValue = grp.Value Then
With ctl.Controls(0)
.ForeColor = xC
.FontWeight = xW
End With
Else
With ctl.Controls(0)
.ForeColor = zC
.FontWeight = zW
End With
End If
End If
Next


Exit_ErrTrack:
Exit Function
ErrTrack:
MsgBox Err.Number & vbLf & Err.Description, , iMsg
Resume Exit_ErrTrack
End Function
 
A

Allen Browne

Hi Jose. That's great, and thanks for posting the result.

Each control has a Controls collection - the controls attached to it. For
most controls, the only attached control is its label, so the controls
collection has only one item. Controls(0) is shorthand for the first item in
the Controls collection, i.e.:
MyControl.Controls.Item(0)

You can tweak your function a little. At a quick scan, I don't think you are
using the frm variable. You can also speed it up a little by not setting the
color if it is already set correctly, since writing a property is much
slower than reading it (and can also cause flicker when the screen is
re-written.)
 

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