highlight field function

xaviermobius

Registered User.
Local time
Today, 23:45
Joined
Jul 25, 2006
Messages
12
I am trying to code a function which will highlight the text box while its being edited.

The function highlight() is placed in the OnGotFocus property of the field so it is called when the field is in focus.

I am new to VBA and dont know how to select the currently selected field i.e. the field which the cursor is currently in.

Function highlight()
Dim highlightcolour As Long
highlightcolour = RGB(255, 255, 0)
Selection.BackColor = highlightcolour
End Function

As you can see above i need to replace the word Selection with the command to select the currently selected field.

Any help would be greatly appreciated.
 
Hi

I'm not sure why you want to use function, but here's something that works.
These are subroutines that do and undo the highlighting. Note that form and control names are input parameters.
Code:
Sub Highlight(ByVal frm, ctrl As String)
    Dim HighlightColour As Long
    HighlightColour = RGB(255, 255, 0)
    Forms(frm).Controls(ctrl).BackColor = HighlightColour
End Sub

Sub UnHighlight(ByVal frm, ctrl As String)
    Dim BaseColour As Long
    BaseColour = RGB(255, 255, 255)
    Forms(frm).Controls(ctrl).BackColor = BaseColour
End Sub


These are the event (GotFocus / LostFocus) handler routines of the editbox.
These should be set separately for every editbox you want to use this way.
Code:
Private Sub MyEditbox_GotFocus()
    Call Highlight(Me.Name, MyEditbox.Name)
End Sub

Private Sub MyEditbox_LostFocus()
    Call UnHighlight(Me.Name, MyEditbox.Name)
End Sub

Edit:

And here's a lot simpler solution:
In design mode, select Format menu -> Conditional formatting -> and select getting focus as condition, then set the background color for meeting the condition.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom