How to change backcolor (but not when first opening the Form)

reddevil1

Registered User.
Local time
Today, 03:28
Joined
Nov 12, 2012
Messages
92
Please could someone advise how I can change the backcolor of a Control to a specified colour when a different Control receives the Focus, APART from when the Form is first opened?

I have the following Controls on my Form:-
ContractID
ContractDetails

When my Form first opens, ContractID receives the Focus (and this is what I want to happen).
I want the backcolor of ContractDetails to change to 9170175 but Not when the Form is first opened. But I do want it to change colour for every other time that ContractID receives the Focus.

This is my current coding:-


Code:
Private Sub ContractID_GotFocus()
ContractDetails.BackColor = 9170175
End Sub

Private Sub ContractID_LostFocus()
ContractDetails.BackColor = 9800909 (this is the standard colour that I want always, unless ContractID has the focus)
End Sub

I tried to add coding to Form On Current event but this codes seems to be overridden by the opening of the Form when I make ContractID receive the Focus (TabIndex 0).
 
Try

Me.ContractDetails.BackColor = 9170175
 
Try

Me.ContractDetails.BackColor = 9170175

I have put this is the GotFocus event....and it works great.

But when the Form first opens, i want the colour to be 9800909
 
I have put this is the GotFocus event....and it works great.

But when the Form first opens, i want the colour to be 9800909

You could use 9800909 in the Properties.

If you want it to change back on opening a new/different record use the Forms On Current Event.
 
May be a better solution, but this should work...
Code:
Private Sub ContractID_GotFocus()
  Static Accumulate As Integer
  Accumulate = Accumulate + 1
  If (Accumulate Mod 2) = 0 Then
    ContractDetails.BackColor = 9170175
  Else
    ContractDetails.BackColor = 9800909
  End If
End Sub

On second thought... this solution is much simpler:
Code:
Option Compare Database
Option Explicit

Public Recolor As Boolean

Private Sub ContractID_GotFocus()
  If Recolor = True Then
    ContractDetails.BackColor = 9170175
    Recolor = False
  Else
    ContractDetails.BackColor = 9800909
    Recolor = True
  End If
End Sub

Private Sub ContractID_LostFocus()
  ContractDetails.BackColor = 9800909
End Sub
 
Last edited:
RedDevil.

What does this mean.
But I do want it to change colour for every other time that ContractID receives the Focus.
You say every other time. Does that mean even though you don't leave the current record.

Please Clarify.
 
MAYBE:
Private Sub ContractID_GotFocus()
If ContractDetails.BackColor = 9170175 Then
ContractDetails.BackColor = 9800909
Else
If ContractDetails.BackColor =9800909 Then ContractDetails.BackColor = 9170175
End If
End Sub
 
reddevil1

I must be slow today as I just realised that you are showing the Primary Key.

A Primary Key should never be available to a user. In all my DBs the Primary Key as well as the FK is never seen by the user. They are there as a unique reference and to join one table to another.

So my suggestion is not to show it.
 
Sorry guys - I have been sick for a few days - but thanks very much for all your suggestions. They are very much appreciated.
 
sjr1917 - i have not come across any of your suggestions before. but they seem like they could work - i will try.

thanks a lot.
 
as far as i know, this is the way the code works:-

1) Form OnCurrent - sets ContractDetails backcolor to Pink
2) ContractID OnGotFocus - sets ContractDetails backcolor to Yellow (this seems to override the Form OnCurrent coding)

So when i first open the Form i want ContractID to be in Focus
But when i first open the Form, i want ContractDetails backcolor to be Pink

I have tried to change the coding but i seem to be going round in circles :(
 
A Primary Key should never be available to a user.
Thanks RainLover - you have put a lot of effort into this thread - cheers.
My thinking was that this the ContractID would be used as the Contract Number - that way the Contract Number would be unique???
 
What does this mean. Quote:
But I do want it to change colour for every other time that ContractID receives the Focus.
You say every other time. Does that mean even though you don't leave the current record.

Yes, even though i do not leave the current record.
i want the ContractDetails backcolor to be Yellow every time the ContractID is in Focus (apart from the very first time the Form opens).

Hope that clarifies.
 
i want the ContractDetails backcolor to be Yellow every time the ContractID is in Focus (apart from the very first time the Form opens).

I read your "every other time" in the first post:
I do want it to change colour for every other time that ContractID receives the Focus.
in terms of every "OTHER" time... one time normal, next time colored, next time normal.

So, if I now understand you right the form in the attached database should do what you're after.
 

Attachments

Thanks RainLover - you have put a lot of effort into this thread - cheers.
My thinking was that this the ContractID would be used as the Contract Number - that way the Contract Number would be unique???

One day you will see the benefits of hiding the Key.

Personally I always use Autonumber as the PK. It is never displayed to the user.

As far as your current problem. You question is confusing. Just about every one has a different opinion of what you want.

The answer will be simple once the objective is properly understood.

It is a strange request anyway. Why would you want to change colours. I am sure you must have a logical reason.

Users do not like this type of thing. If they spend a long time looking at this type of thing they get frustrated. Some even develop headaches. But if it is very suttle then you can get away with it.
 
sjr1917 - thanks for your sample database - i think i should have uploaded one myself a long time ago?

your sample DB works 90%. thank you. the last thing i need is to keep all the same apart from:-
a) when the Form is first opened, and the ContractID is Clicked, then the ContractDetails turn yellow (currently it stays Pink).

i have uploaded my modified version so you (and RainLover) can also see why i want to change colours.

thank guys.
 

Attachments

RedDevil

I can't open anything later than 2003. Same as a lot of the older people here.

If you like you can convert to 2003 and repost.
 
oops, no i haven't :(

i will try this at work tomorrow - it seems my home PC is not able to do this.
 
Ok, reddevil1, IF I understand what you're after then just add onChange event code like this to your ContractID control.

Private Sub ContractID_Change()
ContractDetails.BackColor = 9170175 ' Yellow focused color
End Sub

The ContractID_GotFocus code with its "Is Recolor = True" keep the ContractID from changing color in the form openning, but then the Change event turns it to yellow, even the first time it's touched.

It that what you're trying to accomplish?
 

Users who are viewing this thread

Back
Top Bottom