473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to change option group button ForeColor dynamically?

tuxalot
200 100+
Another quick question for ya...

Here's my button code, and I want to see if I can do something different so I don't have to repeat the same code for each of my 14 buttons in my option group:

Expand|Select|Wrap|Line Numbers
  1. Private Sub grpS1_LostFocus()
  2.  
  3.     Dim intCount As Integer
  4.     Dim lngRed As Long, lngBlack As Long
  5.     lngRed = RGB(186, 20, 25)
  6.     lngBlack = RGB(64, 64, 64)
  7.  
  8.     intCount = DCount("[RspnsID]", "qxtbChkSectionsComplete")
  9.  
  10.     If intCount > 0 Then
  11.     ' missing questions, change button ForeColor to red
  12.         Me!grpS1.ForeColor = lngRed
  13.         Me!grpS1.PressedForeColor = lngRed
  14.         Me!grpS1.HoverForeColor = lngRed
  15.     Else
  16.         ' questions complete, change button ForeColor to black
  17.         Me!grpS1.ForeColor = lngBlack
  18.         Me!grpS1.PressedForeColor = lngBlack
  19.         Me!grpS1.HoverForeColor = lngBlack
  20.     End If
  21.  
  22. End Sub
  23.  
The buttons in the option group "grpSections" are named grpS1 - grpS14 and represent sections on my questionnaire. The query above uses the value of the option group as criteria with [Forms]![frmMain].[Controls]![grpSections].[value] to calculate the DCount on RspnsID for each section.

So it all boils down to this. Is there a way to put code in the option group's on click or after update event to grab the value of the button that has just lost focus, to run the query above and change THAT button fore color? So for example as a user goes from section 1 to 2 by clicking the grpS2 button the query would check for complete questions in section 1 and if there are questions missed then change the fore color for grpS1.

As always, thanks everyone for your help!
Dec 4 '10 #1
11 5256
ADezii
8,834 Expert 8TB
  1. To change the ForeColor of all ToggleButtons within the grpSections Option Group:
    Expand|Select|Wrap|Line Numbers
    1. Dim ctl As Control
    2.  
    3. For Each ctl In Me.Controls
    4.   If ctl.ControlType = acToggleButton And Left$(ctl.Name, 4) = "grpS" Then
    5.     ctl.ForeColor = vbBlack
    6.   End If
    7. Next
  2. I couldn't figure out an easy way to accomplish your second question, but that doesn't mean that one does not exist. I devised what I feel is a simple Method of retaining a Reference to the Last Toggle Button pressed, and here it goes:
    1. Declare a Form Level variable to hold a Reference to the Last Toggle Button pressed:
      Expand|Select|Wrap|Line Numbers
      1. Private ctlLast As ToggleButton
    2. In the LostFocus() Event of each Toggle Button in the Option Group (2nd Button used for Demo), place the following line of Code to explicitly refer to that Button:
      Expand|Select|Wrap|Line Numbers
      1. Private Sub grpS2_LostFocus()
      2.   Set ctlLast = Me!grpS2
      3. End Sub
    3. To now Change the ForeColor Property of the Last Toggle Button pressed in the Option Group:
      Expand|Select|Wrap|Line Numbers
      1. ctlLast.ForeColor = vbRed
  3. As I stated earlier, an easier approach may exist, so keep monitoring this Thread.
  4. Any questions, feel free to ask.
Dec 4 '10 #2
tuxalot
200 100+
Oops. spoke too soon. Seems to change all to red not taking into account if there are missed questions. Testing, be back in a bit with more.


Working now. Set this as a public variable:
Expand|Select|Wrap|Line Numbers
  1. Public Sub changeColor(ctrl As Access.Control)
  2.     Dim intCount As Integer
  3.     Dim lngRed As Long, lngBlack As Long
  4.     lngRed = RGB(186, 20, 25)
  5.     lngBlack = RGB(64, 64, 64)
  6.  
  7.     intCount = DCount("[RspnsID]", "qxtbChkSectionsComplete")
  8.  
  9.     If intCount > 0 Then
  10.     ' missing questions, change button ForeColor to lngRed
  11.         With ctrl
  12.           .ForeColor = lngRed
  13.           .PressedForeColor = lngRed
  14.           .HoverForeColor = lngRed
  15.        End With
  16.     Else
  17.         ' questions complete, change button ForeColor to lngBlack
  18.         With ctrl
  19.           .ForeColor = lngBlack
  20.           .PressedForeColor = lngBlack
  21.           .HoverForeColor = lngBlack
  22.         End With
  23.     End If
  24. End Sub
  25.  
Then, in the option groups after update event, this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Select Case Me.grpSections.Value
  3.         Case 1
  4.             Call changeColor(Me.grpS1)
  5.         Case 2
  6.             Call changeColor(Me.grpS2)
  7.         Case 3
  8.             Call changeColor(Me.grpS3)
  9.         .
  10.         .
  11.         .
  12.            to case 15
  13.     End Select
  14.  
Then in the forms open event, this, to change the default color to black (also put this in a few buttons on the form when changing records):

Expand|Select|Wrap|Line Numbers
  1.     Dim intCount As Integer
  2.     Dim ctl    As Access.Control
  3.     Dim lngRed As Long, lngBlack As Long
  4.  
  5.     lngRed = RGB(186, 20, 25)
  6.     lngBlack = RGB(64, 64, 64)
  7.  
  8.     intCount = DSum("intSections", "qDSections2")
  9.     For Each ctl In Me.Controls
  10.         If TypeOf ctl Is ToggleButton Then
  11.             If ctl.Tag > intCount Then
  12.                 ctl.Visible = False
  13.             Else
  14.                 ctl.Visible = True
  15.                 With ctl
  16.                     .ForeColor = lngBlack
  17.                     .PressedForeColor = lngBlack
  18.                     .HoverForeColor = lngBlack
  19.                 End With
  20.             End If
  21.         End If
  22.     Next ctl
  23.  
So on to the next challenge!
Dec 4 '10 #3
tuxalot
200 100+
ok. Code is updating the fore color but it seems to change from black to red with sections that have unanswered questions and when all the questions are answered you have to click back through each section to get the color to switch from red (missed questions) to black. So maybe a combo approach will do the trick.

Private variable at form level set as such:
Expand|Select|Wrap|Line Numbers
  1. Private ctlLast As ToggleButton

... code for the option group after update event:
Expand|Select|Wrap|Line Numbers
  1. Private Sub grpSections_AfterUpdate()  
  2.     Call changeColor(ctlLast)
  3. End Sub
  4.  
... button lost focus events (on each button)
Expand|Select|Wrap|Line Numbers
  1. Private Sub grpS1_LostFocus()
  2.   Set ctlLast = Me!grpS1
  3. End Sub
  4.  
... and the function
Expand|Select|Wrap|Line Numbers
  1. Public Function changeColor(ctlLast)
  2.     'Dim ctlLast As ToggleButton
  3.     Dim intCount As Integer
  4.     Dim lngRed As Long, lngBlack As Long
  5.     lngRed = RGB(186, 20, 25)
  6.     lngBlack = RGB(64, 64, 64)
  7.  
  8.     Debug.Print ctlLast '<<<<< error here, "entered expression that has no value"
  9.  
  10.     intCount = DCount("[RspnsID]", "qxtbChkSectionsComplete")
  11.  
  12.     If intCount > 0 Then
  13.     ' missing questions, change button ForeColor to lngRed
  14.         With ctlLast
  15.           .ForeColor = lngRed
  16.           .PressedForeColor = lngRed
  17.           .HoverForeColor = lngRed
  18.        End With
  19.     Else
  20.         ' questions complete, change button ForeColor to lngBlack
  21.         With ctlLast
  22.           .ForeColor = lngBlack
  23.           .PressedForeColor = lngBlack
  24.           .HoverForeColor = lngBlack
  25.         End With
  26.     End If
  27. End Function
  28.  
Seems variable has no value?
Dec 4 '10 #4
ADezii
8,834 Expert 8TB
Works fine from this end, are you sure that you are setting the Value of ctlLast in the 'LostFocus()' Event and for each Toggle Button grpS1 thru grpS14?
Dec 4 '10 #5
tuxalot
200 100+
yep. btw, I only have some of the toggles visible when the form loads, depending on how many sections I have in the current survey. I don't think this would be causing the issue. This code for the forms on open event is
Expand|Select|Wrap|Line Numbers
  1.     Dim intCount As Integer
  2.     Dim ctl    As Access.Control
  3.     Dim lngRed As Long, lngBlack As Long
  4.  
  5.     lngRed = RGB(186, 20, 25)
  6.     lngBlack = RGB(64, 64, 64)
  7.  
  8.     intCount = DSum("intSections", "qDSections2")
  9.     For Each ctl In Me.Controls
  10.         If TypeOf ctl Is ToggleButton And Left$(ctl.Name, 4) = "grpS" Then
  11.             If ctl.Tag > intCount Then
  12.                 ctl.Visible = False
  13.             Else
  14.                 ctl.Visible = True
  15.                 With ctl
  16.                     .ForeColor = lngBlack
  17.                     .PressedForeColor = lngBlack
  18.                     .HoverForeColor = lngBlack
  19.                 End With
  20.             End If
  21.         End If
  22.     Next ctl
  23.  
Dec 4 '10 #6
ADezii
8,834 Expert 8TB
  1. Is the code in the Form's Open() Event working as it should?
  2. intSections in the DSum() Function appears to be a Variable Name, is it an actual Field contained in qDSections2?
    Expand|Select|Wrap|Line Numbers
    1. intCount = DSum("intSections", "qDSections2")
Dec 5 '10 #7
tuxalot
200 100+
Yes it is working. I've tried shutting down the db with red colored buttons and it opens with black. Also, I've added/subtracted sections on my survey and the representative sections buttons come and go as expected.

So the last code I posted is working on your end? Very strange. I am running A2010 (but tested it on 07 as well with same results).

Here's the two queries mentioned:

qDSections1
Expand|Select|Wrap|Line Numbers
  1. SELECT tblQuestions.SectionID, Count(tblQuestions.SectionID) AS CountOfSectionID
  2. FROM tblQuestions
  3. WHERE (((tblQuestions.QstnIsActive)=True))
  4. GROUP BY tblQuestions.SectionID;
  5.  
qDSections2
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS intSections
  2. FROM qDSections1;
  3.  
Dec 5 '10 #8
tuxalot
200 100+
Maybe it is where I placed the variable reference. This code
Expand|Select|Wrap|Line Numbers
  1. Private ctlLast As ToggleButton
I placed in the declaration section of Form_frmMain (just above Option Explicit). Should it not be Public instead?

I CAN pass the tag value of the button as a string variable so maybe I can work from that angle. Not sure so any help is appreciated.
Dec 5 '10 #9
tuxalot
200 100+
All is well now. I ditched the function and instead placed this code in the groups after update event
Expand|Select|Wrap|Line Numbers
  1.     Dim intCount As Integer
  2.     Dim lngRed As Long, lngBlack As Long
  3.     lngRed = RGB(186, 20, 25)
  4.     lngBlack = RGB(64, 64, 64)
  5.  
  6.     intCount = DCount("[RspnsID]", "qxtbChkSectionsComplete")
  7.  
  8.     If intCount > 0 Then
  9.         ' missing questions, change button ForeColor to lngRed
  10.         With ctlLast
  11.             .ForeColor = lngRed
  12.             .PressedForeColor = lngRed
  13.             .HoverForeColor = lngRed
  14.         End With
  15.     Else
  16.         ' questions complete, change button ForeColor to lngBlack
  17.         With ctlLast
  18.             .ForeColor = lngBlack
  19.             .PressedForeColor = lngBlack
  20.             .HoverForeColor = lngBlack
  21.         End With
  22.     End If
  23.  
  24.  
The buttons themselves have this as their lost focus event as you provided
Expand|Select|Wrap|Line Numbers
  1. Private Sub grpS1_LostFocus()
  2.     Set ctlLast = Me.grpS1
  3. End Sub
  4.  
and this as the variable declaration
Expand|Select|Wrap|Line Numbers
  1. Private ctlLast As ToggleButton
Thanks for the help once again ADezii.
Dec 5 '10 #10
tuxalot
200 100+
arghh!

So now I need to get the VALUE of ctlLast to pass it as a criteria in my query.
Expand|Select|Wrap|Line Numbers
  1. intCount = DCount("[RspnsID]", "qxtbChkSectionsComplete")
  2.  
  3.     If intCount > 0 Then
  4.         ' missing questions, change button ForeColor to lngRed
  5.         With ctlLast
  6.             .ForeColor = lngRed
  7.             .PressedForeColor = lngRed
  8.             .HoverForeColor = lngRed
  9.         End With
  10.     Else
  11.         ' questions complete, change button ForeColor to lngBlack
  12.         With ctlLast
  13.             .ForeColor = lngBlack
  14.             .PressedForeColor = lngBlack
  15.             .HoverForeColor = lngBlack
  16.         End With
  17.     End If
Right now, the query gets it's criteria value from
Expand|Select|Wrap|Line Numbers
  1. [Forms]![frmMain].[Controls]![grpSections].[value]
so it is the current value of the control, not the last value.

Seems I'm chasing my tail ;)
Dec 5 '10 #11
tuxalot
200 100+
Still chasing, but getting somewhere I think. After some study I settled on the following which works but seems clunky. Let me know what you think.

In the lost focus of each option button I put this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub grpS1_LostFocus()
  2.     Set ctlLast = Me.grpS1 'grabs the name of the button
  3.     intLastValue = Me.grpSections.Value 'grabs the value of the button
  4.     setLastValue intLastValue
  5. End Sub
  6.  
Then added this:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Private intLastValue As Integer
  3. Public Sub setLastValue(Value As Integer)
  4.     intLastValue = Value
  5. End Sub
  6.  
And this:
Expand|Select|Wrap|Line Numbers
  1. Public Function GetLastValue()
  2.     GetLastValue = intLastValue
  3. End Function
  4.  
Then put this in as a criteria in my query:
Expand|Select|Wrap|Line Numbers
  1. GetLastValue()
The after update event for the option group remained unchanged and is shown above.

This is WAY over my head but I'm beginning to see the light. Seems like I've got an extra step in here somewhere.

Nite all.
Dec 5 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: What-a-Tool | last post by:
I have a school project (ASP) in which I have to call three different ASP pages from three different and identical (except for the form "action", obviously) HTM pages. This I have no problem with....
2
by: Corrine | last post by:
Is there a way to cancel a change in an option group? The option group still changes to the option clicked on when clicking on the NO button with the following code in the BeforeUpdate event of the...
1
by: kufre | last post by:
I need some help. I have a option group button that contain two checkbox call Active and Inactive on my Filing form. This is what I want, the Active box will alway be the default checkbox,the...
11
by: MLH | last post by:
Why is that? If I choose the tiny check boxes which are hard to hit with a mouse, it works fine. But option buttions, shich can be sized big enough for people with limited sight and dexterity...
4
by: AA Arens | last post by:
I use a sewt of buttons, created from the "Option Group". I would like to have the pushed button appearing in another color, so it is more clearier which button is pushed. How to proceed it? I am...
1
by: hackerslacker | last post by:
I have an ordering form that use two combo boxes to filter down the records of a Products table. This worked fine with the after_Update of the first filtering the records and creating the...
7
by: sparks | last post by:
When you enter an option group you can move to each option with the arrow keys and select with the space bar. Option groups don't have keydown events so has anyone come up with a way to allow...
5
by: Student1000 | last post by:
Help me out please... Ok, i have been trying to do this for months….I kinda gave up on it but am back at it again. I created a form for a table (Bills). In the form, I have three option buttons...
4
by: sparks | last post by:
well I am trying to get around looking at all the buttons with a case statement so I was trying this. Public Sub looking(ctlname) Dim ctl As OptionGroup Dim btn As ToggleButton ctl =...
2
by: sara | last post by:
Hi I'm having a very strange problem and need HELP!! I have a form for the user to choose a report (radio button in an option group) and parameters (Dates, Season). Click the option button,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.