Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2009
    Posts
    223

    Unanswered: How to change background colour of two Controls

    What coding do i use (and which Event do I put it) if i want to change the background colour of 2 Controls to light yellow when the first control has the focus?

    I have tried many different ways but now I am too frustrated!!!

    Thanks if you can help.
    What would you attempt to do if you knew you would not fail?

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    place the code in that first controls 'on got focus' event
    eg

    Code:
    private sub MyFirstControl_GotFocus()
    MyFirstControl.backcolor = vbYellow
    MySecondControl.backcolor = vbYellow
    end sub
    you'd probably also want to reset the colours to whatever your normal background colour is. to do that place some code in the forms on current event
    Code:
    Private Sub Form_Current()
    'either set it to a specific value such as
    MyFirstControl.backcolor = vbWhite
    MySecondControl.backcolor = vbWhite
    End Sub
    OR be a bit devious and set it to another controls colour, so that if the user changes their theme it picks up the them value, eg:-
    MyFirstControl.backcolor = MyOtherControl.BackColor
    MySecondControl.backcolor = MyOtherControl.BackColor

    you could probably use System Window (thats in A2010 but Im not to certain how your expresss that in VBA)


    However Id advise caution on changing colours and other things. some people stuff the form with different colours, flashing controls and so on. be cautious in the use of such tricks. its a powerful tool to draw attention to a problem, but don't overuse it as it quickly looks a mess.
    choice of colours can also be hazardous, some people are colour blind to specific shades or colours (i'm not aware of anyone who is colour blind to yellow (Usually its blue/green), but y'never know).
    Another issue is that users may have changed their layout theme to use different colours to yours. unless you know they are not using the colours you choose then you loose the effect.
    Last edited by healdem; 11-30-12 at 07:55.
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Nov 2009
    Posts
    223
    healdem,

    thanks for your very useful advice.

    i am currently having aproblem with even setting the background of one control (but hope to sort that soon). Error: Th eobject doesn;t contain the automation object vbYellow?

    I am very uncertain about delving into VBA/SQL coding using PrivateSubForm and EndSub etc

    Would the effect be the same if I just typed the following into the On Got Focus line in the Property Sheet??

    MyFirstControl.backcolor = vbYellow
    MySecondControl.backcolor = vbYellow
    What would you attempt to do if you knew you would not fail?

  4. #4
    Join Date
    Nov 2009
    Posts
    223
    Hey, I tried using Code Builder (for the first time ever) and the highting of 2 controls worked. I amazed myself (haha).

    But i wonder why even 1 control does not highlight when i type into the On Got Focus line

    =MyControl.backcolor - vbYellow

    ???
    What would you attempt to do if you knew you would not fail?

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    =MyControl.backcolor - vbYellow

    I'm not surprised that is causing problems
    you are subtracting the value of the intrinsic vb constant (VBYellow) from the current background colour of your control

    S to whether you can assign a value to a control in the event properties.. I dunno. never tried it. the code I provided you works if you click the on got focus event and select build event|code
    the Access whoudl switch to the code window and display the sub stub

    private mycontrol(whatever your control is called)_GotFocus()


    end sub

    ...just stiuff the relevant code into that sub and away y'go
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Nov 2009
    Posts
    223
    Oops, sorry, there was a typo in my last message. I meant ot put a "=" not at "-".
    =MyControl.backcolor = vbYellow

    ...just stiuff the relevant code into that sub and away y'go
    Whehey, it works and I feel like a fully qulaified computer programmer (LOL). After your help, I feel I have taken a big leap forward.

    Cheers,.
    What would you attempt to do if you knew you would not fail?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •