?
Solved

Change button text color on option group change.

Posted on 2015-01-21
7
Medium Priority
?
576 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 1000 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 38

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 1000 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
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.

777 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