Change Color of active control

AnnusHorribilis

Registered User.
Local time
Today, 03:49
Joined
Apr 12, 2005
Messages
13
Please can someone tell me how to change the back colour of a control when it has the focus and then revert to the original colour when it loses it. I have 35 controls on my form, so I need a function, rather than changing the colour with a subroutine evey time.

I have searched the site and haven't found whatI want-though I'm sure this question has been asked before.
 
search this forum for conditional formatting
 
MSTEF, thanks...

...but I need a generic function I can call from a standard module, so I don't clutter up the form module with masses of code. I know how to do what I want at form level, but, as a VBA beginner,I'm unsure of the syntax for referring to a control in a module.But thanks, anyway.
 
MaxMangion, I forgot to say.....

....most of the controls are option groups and conditional formatting doesn't work with them. All I really need to know is how to refer to a control on a form from within a function in a standard module. Should I post this in another section?
 
You are overconstraining yourself a little. No, make that A LOT.

I need a function, rather than changing the colour with a subroutine evey time.

But the event routines (including GetFocus and LostFocus) are subroutines that respond when you trigger the event. Without a subroutine you have no place from which to CALL the proposed function.

There is no "property" you can set, if that's what you mean. And you cannot cut/paste everything. You have to customize the control names. But you could make a little subroutine to do it if you wanted to. I question whether it is more efficient since all you need is a one-liner in either case.

MyControl_GetFocus()

MyControl.BackColor = {chosen color}

End Sub

MyControl_LostFocus()

MyControl.BackColor = {chosen color}

End Sub
 
Docman,

Ok, you still have to call the function, but that involves typing 'Call FunctionName' 70 times instead of typing all the control names and colour codes 70 times.! But as I've already done that, perhaps I'll just forget doing it any other way. Thanks for your input.
 
Would something like this do?

Behind the Form: -

Code:
Option Explicit
Option Compare Text


Private Sub Form_Open(ByRef intCancel As Integer)
    
    InitializeGotLostFocus Me

End Sub


And in a standard module: -

Code:
Option Explicit
Option Compare Text


Public Const conGotFocusColor As Long = vbRed


Public Sub InitializeGotLostFocus(ByRef frmThisForm As Form)
    Dim lngIndex   As Long
    Dim ctlControl As Control
    
    For Each ctlControl In frmThisForm
        With ctlControl
            Select Case .ControlType
                Case acOptionGroup
                    For lngIndex = 1 To .Controls.Count - 2 Step 2
                        frmThisForm(.Controls.Item(lngIndex).Name).OnGotFocus = MakeFunctionCall("HandleFocusChange", frmThisForm.Name, .Controls.Item(lngIndex + 1).Name, "Got Focus")
                        frmThisForm(.Controls.Item(lngIndex).Name).OnLostFocus = MakeFunctionCall("HandleFocusChange", frmThisForm.Name, .Controls.Item(lngIndex + 1).Name, "Lost Focus")
                        frmThisForm(.Controls.Item(lngIndex + 1).Name).BackStyle = 1
                    Next lngIndex
                
                Case acTextBox
                    .OnGotFocus = MakeFunctionCall("HandleFocusChange", frmThisForm.Name, .Name, "Got Focus")
                    .OnLostFocus = MakeFunctionCall("HandleFocusChange", frmThisForm.Name, .Name, "Lost Focus")
                    .BackStyle = 1
                
            End Select
        End With
    Next ctlControl

End Sub


Public Function HandleFocusChange(ByVal strFormName As String, _
                                  ByVal strControlName As String, _
                                  ByVal strGotLost As String)
    Static lngBackColor As Long
    
    If strGotLost = "Got Focus" Then
        lngBackColor = Forms(strFormName)(strControlName).BackColor
        Forms(strFormName)(strControlName).BackColor = conGotFocusColor
    Else
        Forms(strFormName)(strControlName).BackColor = lngBackColor
    End If
    
End Function


Public Function MakeFunctionCall(ByVal strFunctionName As String, _
                            ParamArray ArgList() As Variant) As String
    Dim lngElement  As Long
    Dim strFunction As String
    
    '   The first argument is NOT optional.
    strFunction = "=" & strFunctionName & "("
    
    '   All the remaining arguments are optional.
    '   Loop through argument range, if passed.
    For lngElement = LBound(ArgList) To UBound(ArgList)
        strFunction = strFunction & Chr$(34) & ArgList(lngElement) & Chr$(34) & ", "
    Next lngElement
                                 
    '   Did we receive any arguments?
    '   If so, trim off trailing ", ".
    If Right$(strFunction, 2) = ", " Then
        strFunction = Left$(strFunction, Len(strFunction) - 2)
    End If
    
    MakeFunctionCall = strFunction & ")"
                                 
End Function
Little A97 demo attached.

Hope that helps.

Regards,
Chris.
 

Attachments

hello​

Cannot open a database created with a previous version of your application​

 
of course it is an Old db.
to change color of current control (textbox) use Conditional Formatting.
 

Users who are viewing this thread

Back
Top Bottom