Enumerating Option Group Controls

  • Thread starter Thread starter Guest
  • Start date Start date
Does anyone have a code snippet showing how to enumerate the controls in an
Option Group?

dim obj as object

for each obj in me.optionGroupControl.Controls
debpug.print obj.name
next
 
This example shows how to enumerate the option buttons in a group, showing
the Name, Option Value, and the Caption of its attached label:

Function EnumOptionGroup(grp As OptionGroup)
Dim ctl As Control
For Each ctl In grp.Controls
If ctl.ControlType = acOptionButton Then
Debug.Print ctl.Name, ctl.OptionValue, ctl.Controls(0).Caption
End If
Next
End Function
 
Thanks for the examples. One other question. What I'm trying to do is
change the forecolor of the labels. Thanks to your example, I can loop
through the controls and identify which label I want to change. Once I have
the object name, what syntax allows me to change the forecolor.

thanks

Allen Browne said:
This example shows how to enumerate the option buttons in a group, showing
the Name, Option Value, and the Caption of its attached label:

Function EnumOptionGroup(grp As OptionGroup)
Dim ctl As Control
For Each ctl In grp.Controls
If ctl.ControlType = acOptionButton Then
Debug.Print ctl.Name, ctl.OptionValue, ctl.Controls(0).Caption
End If
Next
End Function
 
Once you have the reference to the control, it's:
ctl.ForeColor = RBG(255,0,0)

If you are trying to do this for the Caption of the Label of the option
button that has the Option Value of the group:
If grp = ctl.OptionValue Then
ctl.ForeColor = vbGreen
Else
ctl.ForeColor= vbBlack
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Dennis said:
Thanks for the examples. One other question. What I'm trying to do is
change the forecolor of the labels. Thanks to your example, I can loop
through the controls and identify which label I want to change. Once I
have
the object name, what syntax allows me to change the forecolor.

thanks
 
Allen,

I've been testing with your code examples as follows:

Function EnumOptionGroup(grp As OptionGroup)
Dim ctl As Control, myred As Long, myblack As Long

myred = RGB(255, 0, 0)
myblack = RGB(0, 0, 0)
For Each ctl In grp.Controls
If ctl.ControlType = acOptionButton Then
If grp = ctl.OptionValue Then
ctl.ForeColor = myred
Else
ctl.BackColor = myblack
End If
End If
Next

End Function

I get the error "Object doesn't support the property or method" when it
tries to set the Forecolor property.
 
On Thu, 13 Jan 2005 08:39:12 -0800, Dennis

I have a small function to do this(I use it a lot for sorting of
Continuous Forms)

where all the Labels starts with "optBez" and the optionGroup is
called frmOrdnung

SetLabel me.form,"optBez", , 1, 16, Me.frmOrdnung

this will set the Label optBez1 to optBez16 to the color Colordown and
the Label with the number from frmOrdnung to ColorUp

if I want only one label without an optionGroup to toggle I use

SetLabel me.form,"optBez277",True
this will set the Label optBez277 to the DownColor


I changed it to suit your needs for the colors, have fun with it

Public Sub SetLabel(frm As Form, _
ctlName As String, _
Optional Down As Boolean, _
Optional FromNr As Long = -999, _
Optional ToNr As Long = -999, _
Optional DownNr As Long = -999)
Dim I As Long, LabelColor As Long, isPressed As Integer
Dim ColorDown As Long, ColorUp As Long
ColorDown = RGB(255, 0, 0)
ColorUp = RGB(0, 0, 0)
LabelColor = IIf(Down, ColorDown, ColorUp)

'LabelColor = IIf(Down, ACC.ColorDown, ACC.ColorUp)
isPressed = IIf(Down, 2, 1)

On Error GoTo AbBruch

With frm
If FromNr = -999 And ToNr = -999 Then
With .Controls(ctlName)
.SpecialEffect = isPressed
.BackColor = LabelColor
End With
Else
If FromNr = -999 Then FromNr = 1
If ToNr = -999 Then ToNr = FromNr
On Error Resume Next
For I = FromNr To ToNr
With .Controls(ctlName & I)
If I = DownNr Then
.SpecialEffect = 2
' .BackColor = ACC.ColorDown
.BackColor = ACC.ColorDown
Else
.SpecialEffect = isPressed
.BackColor = LabelColor
End If
End With
Next I
End If
End With
AbBruch:
On Error GoTo 0
End Sub
 
I don't think you can change the color of the option button. You can change the
color of the label. I'm not positive on the code to do that :-(
 
John is correct. You need to change the color of the attached label, not the
option button.

Example:
ctl.Controls(0).ForeColor = vbRed
 
You can change the SpecialEffect to Shadowed and change the BorderColor
of an Option Button.
 

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

Back
Top