ScottyBee45
May 19 2012, 01:04 AM
Hello Everyone,
I have a textbox named txtResult that is an unbound expression that displays either "Correct", "Incorrect", "Too Many Selections" or"No Selection". I have a button named btnDisplayColor whose background color I want to change depending upon the value displayed in the txtResult control. I have added a second button named btnColorTest and have attached the following code to its Click Event:
Private Sub btnColorTest_Click()
Dim lngRed As Long, lngGreen As Long, lngWhite As Long
lngRed = RGB(255, 0, 0)
lngGreen = RGB(0, 255, 0)
lngWhite = RGB(255, 255, 255)
If txtResult = "Correct" Then
btnDisplayColor.BackColor = lngGreen
ElseIf txtResult = "Incorrect" Then
btnDisplayColor.BackColor = lngRed
Else
btnDisplayColor.BackColor = lngWhite
End If
End Sub
Problem:
The background Color of btnDisplayColor does not change when I click btnColorTest. What is going on here? I even placed breakpoints in the code and tested the values of the variables txtResult and btnDisplayColor.BackColor and verified that the variables are taking on the necessary values but the color still does not change?
Yes I know I can use the conditional formatting button but am trying to make this code work to verify that I undertand VB basics on changing object properties before moving to some more advanced code. Any ideas would be appreciated.
Thanks,
Scott
pere_de_chipstick
May 19 2012, 01:15 AM
Hi Scott
There is no backcolor property for standard command buttons, you can only change their forecolor.
hth
theDBguy
May 19 2012, 01:17 AM
Hi Scott,

What version of Access are you using? Please remember to select the version number when posting questions in case it becomes relevant to the discussion.
Background colors can't be changed in some versions of Access, and I don't think Conditional Formatting works with buttons either.
Just my 2 cents...
Peter Hibbs
May 19 2012, 02:43 AM
Hi Scott,
As the others have pointed out, you cannot change the Back Color of a button but you can easily simulate the effect by creating a Label control with the same caption as the button and making it the same size as the button. Place the label behind the button and set the Transparent property of the button to Yes and then just change the color of the label to whatever you want instead of the button. If you also want to simulate the 'button pressed' effect of a button you could change the label's Border Style property in the Mouse Down / Mouse Up events of the button.
Peter Hibbs.
theDBguy
May 19 2012, 10:51 AM
Hi Scott,
Just to clarify the point I was trying to make earlier... You can change the back color of a button in Access 2010 but not in previous versions of Access. So, can you tell us which version of Access you are using?
Just my 2 cents...
ScottyBee45
May 19 2012, 11:42 PM
Hello Guys,
DBGuy, I am using access 2010 but I will try Peter's suggestion of simply using a label control to have an object whose color changes. Does not have to be a button, but just a control of any kind that will fill in with a solid color, in this case background color.
What I find interesting is that when I was writing the code the "intellisense feature" would kick in after I typed a period for the button control and it let me choose BackColor from the pop up menu but of course didn't work.
I will try using a label control and let you know how it works----Thanks---Scott
ScottyBee45
May 20 2012, 12:43 AM
Again, Hello Guys
I am using Access 2010 and was able to get the color of a label change correctly as per Peter's suggestion using the following code:
Private Sub btnColorTest_Click()
Dim lngRed As Long, lngGreen As Long, lngWhite As Long
lngRed = RGB(255, 0, 0)
lngGreen = RGB(0, 255, 0)
lngWhite = RGB(255, 255, 255)
If txtResult = "Correct" Then
lblDisplayColor.BackColor = lngGreen
ElseIf txtResult = "Incorrect" Then
lblDisplayColor.BackColor = lngRed
Else
lblDisplayColor.BackColor = lngWhite
End If
End Sub
But then another question arose out of this.....
I took the same code and placed it in the form's OnCurrent event thinking the same code would be triggered by simply advancing to a new record instead of having to click a button but it did not work. Again, I realize there are easier ways to do this but just wanted a simple visual way to learn more about event procedures and what fires them off.
Thanks again guys for any help----Scott
Peter Hibbs
May 20 2012, 04:03 AM
Hi Scott,
First off, check the Back Style property of the label control, does it say 'Normal' or 'Transparent'?
Second, you don't need to declare the color values (unless you are using them somewhere else, maybe), you can just use the default color codes - vbRed, vbGreen, etc.
Third, you are correct that this bit of code would need to also be in the form's On Current event but if the the control txtResult is unbound, then that would not work since it would not get updated for each new record (unless you have some other code that does that).
I think your code should look something like this :-
If Me.txtResult = "Correct" Then
Me.lblDisplayColor.BackColor = vbGreen
ElseIf Me.txtResult = "Incorrect" Then
Me.lblDisplayColor.BackColor = vbRed
Else
Me.lblDisplayColor.BackColor = vbWhite
End If
I would also suggest you prefix all controls with Me. which will activate the Intellisense feature when you type the code which makes your typing easier and more reliable.
HTH
Peter Hibbs.
pere_de_chipstick
May 20 2012, 04:09 AM
Hi Scott
How does your text box 'txtResult' show the result, if it is unbound (from your first post) then it will presumably display the same result whatever record you are on, hence the on current event when if fires will not appear to make any changes.
btw I checked your initial code for changing the back colour of a button on A2010 (good catch DBguy) and it appears to work correctly, I've also bound the txtResult text box to a field and again the btnDisplayColor command button back colour is changed appropriately by the On Current event when the record is changed .
hth
ScottyBee45
May 20 2012, 11:02 PM
Hello HTH (Pere)
The unbound textbox txtResult pulls the value from a subform and changes everytime I advance to a new record on the main form so shouldn't the code fire in the OnCurrent event of the main form?
Peter, thanks for the tips, they will help later on.
Thanks,
Scott
pere_de_chipstick
May 21 2012, 02:11 AM
Hi Scott
You are probably finding that while the On Current Event fires, it probably fires before the text box has had time to be updated, you will need to update the text box before the button backcolor is changed, which in turn will mean you have to wait for the sub form to be updated when the record is changed.
try in the On Current event (not tested)
Me.YourSubformName.Requery
Me.txtResult.Requery '(Or run whatever code that updates the text box)
'Add code for updating button backcolor
hth (Hope That (or This) Helps)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.