Link to home
Start Free TrialLog in
Avatar of TechGuise
TechGuiseFlag for United States of America

asked on

Change each COMMAND BUTTON's color, on particular forms, based on GOT FOCUS/LOST FOCUS

So I found an old application I did several years ago when I didn't know very much (I know probably .05% more now)  :o)

It looked something like this.........

Private Sub Cmd4_GotFocus()
Cmd4.FontBold = True
Cmd4.ForeColor = 128
Cmd4.FontSize = 14

End Sub
Private Sub Cmd4_LostFocus()
Cmd4.FontBold = True
Cmd4.ForeColor = 0
Cmd4.FontSize = 12

End Sub

Open in new window


So I'm playing around and now that ACCESS let's you change the COLOR of a BUTTON, and gives the ability to make them look "cooler", I thought I would streamline it and use it on a wide scale basis on a current project i'm working on.

But this doesn't work......
Private Sub Cmd5_GotFocus()
'When a BUTTON is SELECTED
Dim stForeSel As String
Dim stBackSel As String
Dim stCtrl As Control

stForeSel = Nz(DLookup("[fieldvalue]", "tblparameters", "[FieldName]='ColorBtnForeSelected'"), 0)
stBackSel = Nz(DLookup("[fieldvalue]", "tblparameters", "[FieldName]='ColorBtnBackSelected'"), "RGB(220, 29, 63)")
stCtrl = Screen.ActiveControl.Name

stCtrl.FontBold = True
stCtrl.ForeColor = stForeSel
stCtrl.FontSize = 14
stCtrl.BackColor = stBackSel

End Sub

Open in new window

(I have a table called tblParameters that I use to hold values in case we want to change various things later)  See pic
Best I can tell, The code above seems to have (at least) two problems
1. The way I am referencing the "control" name doesn't seem to be right.
2. The RGB text string it's picking up isn't right.

We have a several instances where folks mainly use the KB, and arrowing thru the buttons on a menu is easier when the highlighted button "JUMPS" out at you.    

Since the "Conditional Formatting" button is not enabled when a Button is selected, I'm assuming that it only works on Text Boxes.

THanks in advance
Capture.JPG
Avatar of DrTribos
DrTribos
Flag of Australia image

Avatar of Máté Farkas
So you have to ensure that your button name is Cmd5 and set the GotFocus event to this sub with some fixes:
Private Sub Cmd5_GotFocus()
Dim stForeSel As Integer
Dim stBackSel As Integer
Dim stCtrl As Control

stForeSel = Nz(DLookup("[fieldvalue]", "tblparameters", "[FieldName]='ColorBtnForeSelected'"), 0)
stBackSel = Nz(DLookup("[fieldvalue]", "tblparameters", "[FieldName]='ColorBtnBackSelected'"), "RGB(220, 29, 63)")
stCtrl = Screen.ActiveControl

stCtrl.FontBold = True
stCtrl.ForeColor = stForeSel
stCtrl.FontSize = 14
stCtrl.BackColor = stBackSel

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bitsqueezer
Bitsqueezer
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TechGuise

ASKER

So first of all....   THANK YOU Christian!

I appreciate all of the code and will try the Class Module solution another time.
With a few of your first points I was able to get the exact solution I was looking for, although I realize it is not the most efficient way to accomplish the task.   But I do not want to just copy and paste your code and not fully understand what is going on, and at the moment I just don't have the mental capacity or time to wrap my brain around the more lengthy (yet more efficient)  method.

Below is what I ended up using.  For menus that I want this functionality I will cut and paste the exact code for each BUTTON's "GotFocus" and "LostFocus" event, but I don't have to modify the control name of each BUTTON (which is awesome).

Private Sub Cmd5_GotFocus()
'When a BUTTON is SELECTED
Dim stForeSel As String
Dim stBackSel As String
Dim stCtrl As Control

stForeSel = Nz(DLookup("[fieldvalue]", "tblparameters", "[FieldName]='ColorBtnForeSelected'"), 0)
stBackSel = Nz(DLookup("[fieldvalue]", "tblparameters", "[FieldName]='ColorBtnBackSelected'"), "RGB(20, 129, 163)")
Set stCtrl = Screen.ActiveControl

stCtrl.FontBold = True
stCtrl.ForeColor = Eval(stForeSel)
stCtrl.FontSize = 14
stCtrl.BackColor = Eval(stBackSel)

End Sub



Private Sub Cmd5_LostFocus()
'When a BUTTON LOOSES FOCUS
Dim stFore As String
Dim stBack As String
Dim stCtrl As Control

stFore = Nz(DLookup("[fieldvalue]", "tblparameters", "[FieldName]='ColorBtnFore'"), "RGB(255, 255, 255)")
stBack = Nz(DLookup("[fieldvalue]", "tblparameters", "[FieldName]='ColorBtnBack'"), "RGB(204, 153, 0)")
Set stCtrl = Screen.ActiveControl

stCtrl.FontBold = False
stCtrl.ForeColor = Eval(stFore)
stCtrl.FontSize = 12
stCtrl.BackColor = Eval(stBack)

End Sub

Open in new window


Thanks Again
Thank You!