Active control forecolor

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'd like to create a routine that I can use on any Access form where the
active control's fore color property will change to blue when the control has
focus, and back to black when the control loses focus. I've played around
with this for some time, but don't have enough familiarity with the object
model to figure it out. Any help will be greatly appreciated.
 
Hi Ken

I seem to remember it's a bit of a fiddle to set the colour of an object in
VB because you have to define the colours first. I'll try and find some code
that does this.

Dim Black As Long
Black = RGB(0, 0, 0)

Once you've defined a name for your colour, you can use it to set the colour
of controls, e.g.:

[Forms]![Myform]![Mycontrol].ForeColor = Black

Maybe you can even say:

[Forms]![Myform]![Mycontrol].ForeColor = RGB(0,0,0)

without creating a variable, I don't know, give it a try if you like.

Regarding getting the ForeColor to change when it gets or loses the focus,
that's easy-peasy. In the Events tab in Properties for each control, stick
the appropriate code in for the Events called On Got Focus and On Lost Focus.
If you don't know how to put code in for Events, give me a shout back.

Cheers

David
 
Put the following code in the OnFocus event of the control:
Me!NameOfControl.ForeColor = vbBlue

and in the LostFocus event:
Me!NameOfControl.ForeColor = vbBlack
 
Thanks for your response, but your solution would require me putting code in
the Got Focus and Lost Focus events of each control on the form. If there
are thirty or more fields on a form that's a lot of tedious work. I would
like to
have a single routine that would change the fore color properties for
whatever control on the form has the focus and then change it back when it
loses focus.
 
Ken said:
I'd like to create a routine that I can use on any Access form where the
active control's fore color property will change to blue when the control has
focus, and back to black when the control loses focus. I've played around
with this for some time, but don't have enough familiarity with the object
model to figure it out. Any help will be greatly appreciated.


Create a public function in a standard module to set the
active control's Forecolor property:

Public Function SetFore()
Screen.ActiveControl.ForeColor = vbBlue
End Function

Similarly, create another function to set the color you want
when the control doesn't have the ficus.

Now, you can set the OnGotFocus Property of all the controls
to =SetFore() and the OnLostFocus property to the other
function. You can even set all the controls in one shot by
selecting all the control and typing the property.
 
Thanks for your help.

Marshall Barton said:
Create a public function in a standard module to set the
active control's Forecolor property:

Public Function SetFore()
Screen.ActiveControl.ForeColor = vbBlue
End Function

Similarly, create another function to set the color you want
when the control doesn't have the ficus.

Now, you can set the OnGotFocus Property of all the controls
to =SetFore() and the OnLostFocus property to the other
function. You can even set all the controls in one shot by
selecting all the control and typing the property.
 
Ken Warthen said:
Thanks for your response, but your solution would require me putting code in
the Got Focus and Lost Focus events of each control on the form. If there
are thirty or more fields on a form that's a lot of tedious work. I would
like to
have a single routine that would change the fore color properties for
whatever control on the form has the focus and then change it back when it
loses focus.

If you don't mind the controls without focus being the same BackColor as the
form you can use an approach that requires no code at all.

It depends on the behavior of controls wherein the setting of "Transparent" for
the BackStyle property is ignored for the control with focus. So you set all
controls to have a contrasting BackColor and then set the BackStyle to
transparent. The one with focus will automatically show in the contrasting
color.

If you wanted a different color then the form's background for controls without
focus you could place colored rectangles behind them.
 
Back
Top