Solved

Change button text color on option group change.

Posted on 2015-01-21
7
510 Views
Last Modified: 2015-01-21
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

0
Comment
Question by:bhieb
7 Comments
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
ID: 40563182
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
 

Author Comment

by:bhieb
ID: 40563187
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
 

Author Comment

by:bhieb
ID: 40563191
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 34

Expert Comment

by:PatHartman
ID: 40563211
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
 

Author Comment

by:bhieb
ID: 40563213
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
 
LVL 20

Accepted Solution

by:
GrahamMandeno earned 250 total points
ID: 40563219
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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40563224
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

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now