Have an application with numerous forms, each containing multiple
bound text boxes. As the user tabs from field to field down any of the
forms, I want to change the selected text box background color from a
white default to a different color, to make it easy to spot which
field has the focus at any time. I am currently accomplishing this by
using each text box's GotFocus and LostFocus events, each with a
different textbox.backcolor color code which works perfectly. This,
however, requires an enormous amount of labor in a large application,
as two events have to be set for every single text box.
Is there a way to accomplish the same with one block of code elsewhere
in the application, hopefully public code in a module?
Thanks in advance for any help or ideas.
Arsene
You could try this. Place the following code in the form's code module
Private Function SetBackground(blnSet As Boolean) As Long
Dim strCtl As String
Dim lngColor As Long
lngColor = IIf(blnSet, 16711935, 16777215)
strCtl = Me.ActiveControl.Name
Me(strCtl).BackColor = lngColor
End Function
Then highlight all of the textbox controls in the form. Open up the
property sheet. In the GotFocus event enter
=SetBackground(True)
and in the LostFocus event enter
=SetBackground(False)
Run and test.
Worked beautifully, Salad, this is great.
Tried to stick the same code as a Public Function in a module to make
it available to all forms, but the "Me" in:
strCtl = Me.ActiveControl.Name
keeps it from running as there's no underlying objetcs to the module.
Thanks !!!!!!!!!!!!!!
I think you can avoid using Me by using:
Screen.ActiveForm.ActiveControl
But for text and combo boxes you don't need any code. You
can use Comditional Formatting's Field Has Focus option
instead.
--
Marsh
Conditional formatting!! But of course!!!
Thanks again, Salad.
You could try this. Put this in a Module.
Public Function SBC(strForm As String, blnSet As Boolean) As Boolean
Dim strCtl As String
Dim lngColor As Long
lngColor = IIf(blnSet, 16711935, 16777215)
strCtl = Forms(strForm).Form.ActiveControl.Name
Forms(strForm)(strCtl).BackColor = lngColor
End Function
Then highlight the textboxes and enter in the GotFocus even
=SBC("Form4",True)
and
=SBC("Form4",False)
in the ListFocus event
Make them all the highlight color then set all to transparent
backgrounds. The one with focus will highlight with no code at all.
If you insist the ones without focus have a white background just put
white rectangles behind them.
John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
That is clever, Salad, will give it a try in my next Access
application.
Thanks!