Microsoft Access – Highlight the Current Active Form Control

I’ve just been working on a form with several controls and wanted to highlight which control was currently active. I wanted to do this by setting the control’s background colour to yellow when they had focus and setting them back to white when focus was lost.

With a few form controls this would have been as simple as adding a single line to the GotFocus and LostFocus events for each item on the form setting their back colour.

e.g.  Me.txtBoxName.BackColor=11596799 (where 11596799 is yellow)

However, with lots of controls on a form this can get a little tedious and I may want to change the behaviour of this action later (maybe changing yellow to green for example).

As I don’t want to have to do this several times I created two subroutines that do the job of changing the control’s background colours when they get focus or lose focus.

These are:

Sub SetGotFocus()
Set ctl = Screen.ActiveControl
ctl.BackColor = 11596799 ' set background to yellow
End Sub

and

Sub SetLostFocus()
Set ctl = Screen.ActiveControl
ctl.BackColor = -2147483643 ' set background to white
End Sub

Then, for each control that I want to change the background, I call the subroutines from their GotFocus and LostFocus events.

Private Sub txtBoxName_GotFocus()
SetGotFocus
End Sub

Private Sub txtBoxName_LostFocus()
SetLostFocus
End Sub

This isn’t rocket science but at least allows a consistent approach to highlighting a control and allows changes to be made more easily. Also it means I don’t have to type “-2147483643” more than once!

Advertisements

2 Responses to “Microsoft Access – Highlight the Current Active Form Control”


  1. 1 NachoMan November 24, 2008 at 10:24 pm

    If you just want to change background color, why not use conditional formatting? No code involved. Your approach (I use something similar) would be necessary if you wanted to modify borderwidth or color on the fly.

  2. 2 Peter Smith January 28, 2009 at 1:26 am

    Or you could use Allen Browne’s suggestion of running a function (using the immediate window (ctrl-G)) to populate the get and setfocus events of every control on the form (passed into the function as an argument).

    http://www.tech-archive.net/Archive/Access/microsoft.public.access.forms/2004-03/1042.html


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Blog Stats

  • 222,048 hits

Counter

ClustrMap


%d bloggers like this: