Full Version: Change Backcolor When Different Control Receives The Focus
UtterAccess Forums > Microsoft® Access > Access Forms
reddevil1
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).
reddevil1
Thanks for your quick reply, Larry.
When my Form first opens, ContractID receives the Focus (as per Tab Index = 0). This means that the backcolor of ContractDetails changes to 9170175.
This is what I want to happen. But how do i stop it happening when the Form is opened for the first tme?
reddevil1
Thanks again LarryL,
think we will call you the TempVars king from now on (LOL). i will try the coding.
(laughing) i have still been too lazy to revise and learn about TempVars, so i am going in blind on this issue.
cheers.
reddevil1
LarryLofL,
You know, I think I am getting used to this TempVar thingy!!!
Am i correct in assuming that the name of the TempVars "ContractIDFocus" can actually be any name? But obviously, the name has to mean something to the coder?
CODE
Private Sub tblFinance_ContractID_GotFocus()
If TempVars!ContractIDFocus = 1 Then
txtContractDetails.BackColor = 9800909
            
TempVars.Add "ContractIDFocus", 2
Else
txtContractDetails.BackColor = 9170175
  
End If
End Sub

When i first open the Form, the backcolor is correct. Thanks very much for offering a solution for that problem.
But i want the backcolor to change if i immediately again click on ContractID (eg. to change the data in ContractID).
At the moment, i have to click on another control and then click on ContractID before the backcolor will change.
I hope i have explained clearly enough?
reddevil1
hahaha - oh yeh, that is right. But of course i knew that already (NOT).
Actually, this is the first time ever i have used the Click event.
LLL - you have won UA helper of the day.
Thank you.
reddevil1
i have just found out that the TempVars solution bring up an error if it is a New Record.
!--c1-->
CODE
Private Sub TransactionTypeID_Enter()
RememberTransactionTypeID = Me.TransactionTypeID
End Sub

the error is
RunTime error 94
Invalid use of Null
CODE
Private Sub tblFinance_ContractID_GotFocus()        
        TempVars.Add "ContractIDFocus", 2
End Sub

please would anybody be able to help guide me to stop the errror message?
thanks if you can
reddevil1
I didn’t want to overload this reply with too much code. I hope I have included enough?
When it is a New Record, and if I the Users first action is to click on the “empty” TransactionTypeID, then the error message is
RunTime error 94
Invalid use of Null
But if I click “End” on the error message, then I can click again on the TranscationTypeID combo and it works OK.
Ocannot work out where the code sees a Null value?
CODE
Option Compare Database
Option Explicit
Dim RememberTransactionTypeID As String
Private Sub Form_Open(Cancel As Integer)
TempVars.Add "ContractIDFocus", 1
DoCmd.GoToRecord , "", acLast
End Sub
Private Sub TransactionTypeID_Enter()
RememberTransactionTypeID = Me.TransactionTypeID
End Sub
Private Sub TransactionTypeID_AfterUpdate()
If RememberTransactionTypeID <> Me.TransactionTypeID Then
Me.MoneyOutTo = ""
End If
Me.MoneyOutTo.Requery
Me.MoneyOutTo.SetFocus
End Sub
Private Sub tblFinance_ContractID_GotFocus()
TempVars.Add "ContractIDFocus", 2
End Sub
reddevil1
The debuggar highlights the following line as the guilty line:-
ememberTransactionTypeID = Me.TransactionTypeID
reddevil1
I just realised that "ContractIDFocus" has nothing to do with the problem. The problem is related to TransactionID only and whether the "MoneyOutTo" field is Required or set to Zero "". The error message is correct (RunTime error 94
Invalid use of Null)
How to find out where that Null value comes from?
CODE
Option Compare Database
Option Explicit
Dim RememberTransactionTypeID As String
Private Sub TransactionTypeID_Enter()
RememberTransactionTypeID = Me.TransactionTypeID
End Sub
Private Sub TransactionTypeID_AfterUpdate()
If RememberTransactionTypeID <> Me.TransactionTypeID Then
Me.MoneyOutTo = ""
End If
Me.MoneyOutTo.Requery
Me.MoneyOutTo.SetFocus
End Sub
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.