Change button text color on option group change.

Been a while since I took a stab at vba and access, but I have a simple problem.

I have an option group with 6 buttons, when one is pressed I'd like to bold and set the text color to red.

I realise I could use if or case to nest all 6 possible senarios, but seems needlessly messy. Such as this.

Select Case optPlant
    Case 1
        btn1.ForeColor = 255
        btn2.ForeColor = 0

Open in new window


What would seem ideal would be to call a for next loop to loop through all the toggle button options in the frame. Something like this, I just don't know the correct syntax for the controls under an option frame. Just seems like that would be cleaner since adding more buttons just gets really messy with the case way.

for each btn in optPlant.buttons
 if btn = pressed then 
    btn.forecolor =255
 else 
   btn.forecolor = 0
  end if
next

Open in new window

bhiebAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
GrahamMandenoConnect With a Mentor Commented:
You can use the Controls collection of the option group.  Note that the collection includes any attached label, so you must check the ControlType:
Private Sub OptGroupName_AfterUpdate()
Dim ctl As Access.Control
For Each ctl In OptGroupName.Controls
  With ctl
    If .ControlType = acToggleButton Then
      If OptGroupName.Value = .OptionValue Then
        .ForeColor = 255
      Else
        .ForeColor = 0
      End If
    End If
  End With
Next ctl
End Sub

Open in new window


Good luck!
Graham Mandeno [Access MVP 1996-2015]
0
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
try this codes

Private Sub optPlan_Click()
Dim j As Integer, x As Integer
j = Me.optPlan.Value
For x = 1 To 6
    Me("btn" & x).ForeColor = 0
Next
Me("btn" & j).ForeColor = 255
End Sub
0
 
bhiebAuthor Commented:
That would require the buttons to be named a certain way. Doable, but I'd like to keep the program as universal as possible. So the next guy behind me can just add another button and away they go.

Making some headway here on my own.  I can loop the controls like so, but how do I then add the validation to see if the
control is the one pressed and if so turn it red.  

This gives me their names, and from that I can do things such as y.forecolor = 255
For Each c In optPlant.Controls
   y = c.Name
Next c

Open in new window

0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
bhiebAuthor Commented:
Actually 2 validations. One is the control a button (since the frame and label are also there, and if it is a button is it pressed.
0
 
PatHartmanCommented:
Access doesn't have any control arrays and the buttons in an option group don't have a value property.  Only the option group control has a value.  You have to change the label of the button.

            Me.Label51.ForeColor = vbRed

if you rename all the labels so they have a prefix followed by the button value, you should be able to concatenate the two to get the name.  I don't have time to figure out what syntax might work but it would be something like:
Controls("lbl" & Me.OptionGroupName).ForeColor

The problem is that although this will set one of the options to red, it doesn't address the others and I don't know what will happen if you reuse the option group.  Will all the labels eventually turn red?  So, you'll probably need a loop that sets the forecolor back to the default for all the labels.
0
 
bhiebAuthor Commented:
Figured it out Rey. Gonna toss you the points since your way would have worked. Here is the final code.

Dim ctlVar As Control

For Each ctlVar In optPlant.Controls
  If ctlVar.ControlType = acToggleButton Then
      If ctlVar.OptionValue = optPlant Then
        ctlVar.ForeColor = 255
      Else
        ctlvar.ForeColor = 0
      End If
End If
Next ctlVar

Open in new window

0
 
Jeffrey CoachmanMIS LiasonCommented:
I am not great at loops like this but this seems to work OK for me...
Private Sub Form_Current()
    Call SetOptProps
End Sub

Private Sub Frame10_AfterUpdate()
    Call SetOptProps
End Sub

Private Sub SetOptProps()
If Me.Frame10 = 1 Then
    Me.Label14.FontBold = True
    Me.Label14.ForeColor = 255
    
    Me.Label16.FontBold = False
    Me.Label16.ForeColor = 0
    
    Me.Label18.FontBold = False
    Me.Label18.ForeColor = 0
ElseIf Me.Frame10 = 2 Then
    Me.Label14.FontBold = False
    Me.Label14.ForeColor = 0
    
    Me.Label16.FontBold = True
    Me.Label16.ForeColor = 255
    
    Me.Label18.FontBold = False
    Me.Label18.ForeColor = 0
ElseIf Me.Frame10 = 3 Then
    Me.Label14.FontBold = False
    Me.Label14.ForeColor = 0
    
    Me.Label16.FontBold = False
    Me.Label16.ForeColor = 0
    
    Me.Label18.FontBold = True
    Me.Label18.ForeColor = 255

 End If

End Sub

Open in new window


Sample DB is attached, FWIW
Database54.mdb
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.