change forecolor on got/lost focus

qwkslvr1999

Registered User.
Local time
Today, 18:27
Joined
Jan 21, 2002
Messages
42
I can change the forecolor of a control on got/lost focus but I have to rewrite the code over and over for each control.

How can I do it in a function? I tried using the activecontrol property but I don't have any luck to make it work. Thanks in advance for any help.
 
I do this, too. Haven't found a good, clean way to do it yet, either. Besides which, it also matters what kind of control it is as to whether it even HAS a forecolor.
 
I'm only doing this to labels as the associated control (textbox/subform/command button,etc) got/lost focus or on enter/exit properties.
 
Me too. I understand what you were doing.

I ended up doing a lot of cut-'n'-paste plus a little editing.

Theoretically, you could do this with a wizard that is capable of writing code for you - but I've never gotten around to doing this.
 
Oh, well. I guess I just have to continue doing the tedious work and just hope someone will come up with the shortcut. :)
 
This is frustratingly close, I had to use the timer event to call the function though
Function CondFmat()
On Error Resume Next
Dim frm As Form
Dim ctl As Control
Dim ctlA As Control
Set frm = Screen.ActiveForm
Set ctl = Screen.ActiveControl
Set ctlA = Screen.PreviousControl

ctl.Controls(0).ForeColor = vbBlue

ctlA.Controls(0).ForeColor = vbBlack


End Function
 
Thanks, Rich! It does work but not at all times. I wonder why? the onfocus forecolor stays even though the focus is already in another control. And it only happens with the first few controls I set the focus on when the form opens.
 
If you are using A2K you can use the Conditional Formatting (under tools) to set the forecolour on the On Focus event of the field. Works well but not available in 97 I think.
 
Does cond formatting work for the labels Dawn?
qwkslvr, therein lies the frustration, I only had a problem with the first control, I suspect because there is no previous cntl, although if you tab back into it and then out the code works, arghhh
 
Missed that vital piece of information, sorry! I'll check....
 
Nope, sorry! I should have realised there was a reason that you hadn't already suggested it, Rich!

:mad:
 
Rich, that's exactly what happens with me, too.

DBL, I'm using A97. There isn't a condiitonal formatting wizard in A97 but I was able to do a work around by selecting the controls (multiple selection) and then just put in the function name in the enter/exit events.

I found a code that works but I just can't remember where I got it. Here it is:

****************
Public Function ChngLblColor(Optional lngColor As Long = 255) As Boolean
'Changes the controls label color (to red by default)
On Error GoTo Err_this

Screen.ActiveControl.Controls(0).ForeColor = lngColor

Exit_this:
Exit Function
Err_this:
Select Case Err.Number
'This error occurs when activating the form after selecting
'another object.
Case 2474
Resume Exit_this
'Error occurs when control does not have a control (label)
Case 2467
Resume Exit_this
Case Else
MsgBox Err.Number & ", " & Err.Description
Resume Exit_this
End Select

End Function
**************

I just call this function on the controls' enter event :
=ChngLblColor()
and on the exit event, since I want it to go back to black, I do:
=ChngLblColor(0)

It's been working well so far. The labels associated with the control changes colors, not the text in the control itself.

Thanks for all your help!
 
Last edited:

Users who are viewing this thread

Back
Top Bottom