Sunday, August 06, 2017

Highlight the Current Control

Code vs. property


Many users have trouble knowing which text box on a form they're currently working with. One way to make it clear for users is to highlight the current one, for example, with a yellow background.
Access allows you to do this with conditional formatting, but you can also get a similar result using code.

To do so, create a new module and add the following code:

Function Highlight(Stat As String) As Integer
Dim ctrl As Control
On Error Resume Next
Set ctrl = Screen.ActiveControl
If Stat = "GotFocus" Then
ctrl.BackColor = 65535
ElseIf Stat = "LostFocus" Then
ctrl.BackColor = 16777215
End If
End Function

Save and close the module, then open the form you want to apply the highlighting to in Design view.
Click the Code button and insert

Highlight("GotFocus")

in each text box control's GotFocus event procedure. Likewise, add

Highlight("LostFocus")

to each text box's LostFocus event procedure.
When you've finished,save the changes, close the VBE, and switch to Form view.

When you tab to a field, it's shaded yellow. When you tab away from the field, its background is restored to white.



See all Topics

No comments: