Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Access 2003 - easing the task of changing a selected text box background color

58 views
Skip to first unread message

Ars...@selenium.net

unread,
Apr 10, 2011, 1:13:00 PM4/10/11
to
Fellows,

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

Salad

unread,
Apr 10, 2011, 1:50:49 PM4/10/11
to
Ars...@selenium.net wrote:

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.

Ars...@selenium.net

unread,
Apr 10, 2011, 2:17:44 PM4/10/11
to
On Sun, 10 Apr 2011 12:50:49 -0500, Salad <sa...@oilandvinegar.com>
wrote:

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 !!!!!!!!!!!!!!

Marshall Barton

unread,
Apr 10, 2011, 3:27:28 PM4/10/11
to

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

Ars...@selenium.net

unread,
Apr 10, 2011, 3:54:51 PM4/10/11
to

Conditional formatting!! But of course!!!

Thanks again, Salad.

Salad

unread,
Apr 10, 2011, 5:43:03 PM4/10/11
to
Ars...@selenium.net wrote:

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

Rick Brandt

unread,
Apr 10, 2011, 8:58:37 PM4/10/11
to
Ars...@selenium.net wrote:
> 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.

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

unread,
Apr 11, 2011, 9:36:39 AM4/11/11
to
One small modification to Rick Brandt's suggestion.
Instead of putting white rectangles behind each control, just set the Back
Color of the section to white.


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Ars...@selenium.net

unread,
Apr 15, 2011, 1:08:08 PM4/15/11
to
On Sun, 10 Apr 2011 16:43:03 -0500, Salad <sa...@oilandvinegar.com>
wrote:

That is clever, Salad, will give it a try in my next Access
application.

Thanks!

0 new messages