Show field which has focus in different color

  • Thread starter Thread starter Anne
  • Start date Start date
A

Anne

Everyone is asking me to show the field which has focus in a different
color. I am been applying conditional formatting to each individual field.
Is there a way to do this with code?
Anne
 
Everyone is asking me to show the
field which has focus in a different
color. I am been applying conditional
formatting to each individual field.
Is there a way to do this with code?

In the GotFocus event, put code:

Me!theFieldName.Backcolor = xxxyyyzzz

where xxxyyyzzz is a color number (find a color with the color picker on
some existing control, and then copy the number).

In the LostFocus event, put code to change the backcolor to the default.

Larry Linson
Microsoft Access MVP
 
You have to put this in every field?
Isn't there one you can use on open of
the form?

No, not that I know of. There's no "do this whenever any control on the form
gets or loses focus" event for a Form.

You could code a function call in the Got and Lost Focus events, and put the
code in a Form level function. The GotFocus and LostFocus events are how you
know when the particular Control gets and loses focus -- and that is when
you wanted the background color changed, isn't it?

Larry Linson
Microsoft Access MVP
 
Yes, you need to put this in the events of each control, but you can do that
programmatically.

The following example opens the form in design view, and sets the OnGotFocus
and OnLostFocus properties of all the text boxes and combo boxes:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function
 
Anne said:
You have to put this in every field? Isn't there one you can use on open of
the form?
Anne

If you don't mind the controls without focus being the same color as the form
you can set them all to a contrasting color and then set them to transparent.
The transparent setting is ignored for the control that has focus so it will
automatically show a different color (with no code).
 
I like your solution, but I am not sure how to use it.
I put your code into a module, but I do not know what to put on the form to
activate it.
Anne

Allen Browne said:
Yes, you need to put this in the events of each control, but you can do that
programmatically.

The following example opens the form in design view, and sets the OnGotFocus
and OnLostFocus properties of all the text boxes and combo boxes:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
You have to put this in every field? Isn't there one you can use on open
of
the form?
Anne
 
1. Select the module tab of the Database window, and click New.
Access opens a new module.

2. Paste in the code.

3. Open the Immediate window: Ctrl+G

4. Enter:
? SetupHightlight("Form1")
substituting your actual form name for Form1.

The code will run, and set the OnLostFocus and OnGotFocus properties of
controls on the form. These events will call the other function, which
actually does the highlighting and dehighlighting.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
I like your solution, but I am not sure how to use it.
I put your code into a module, but I do not know what to put on the form
to
activate it.
Anne

Allen Browne said:
Yes, you need to put this in the events of each control, but you can do that
programmatically.

The following example opens the form in design view, and sets the OnGotFocus
and OnLostFocus properties of all the text boxes and combo boxes:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function


Anne said:
You have to put this in every field? Isn't there one you can use on
open
of
the form?
Anne


Everyone is asking me to show the
field which has focus in a different
color. I am been applying conditional
formatting to each individual field.
Is there a way to do this with code?

In the GotFocus event, put code:

Me!theFieldName.Backcolor = xxxyyyzzz

where xxxyyyzzz is a color number (find a color with the color picker on
some existing control, and then copy the number).

In the LostFocus event, put code to change the backcolor to the default.

Larry Linson
Microsoft Access MVP
 
Got it, but I need to use it for several forms. Do I create a new module for
every form?
Anne

Allen Browne said:
1. Select the module tab of the Database window, and click New.
Access opens a new module.

2. Paste in the code.

3. Open the Immediate window: Ctrl+G

4. Enter:
? SetupHightlight("Form1")
substituting your actual form name for Form1.

The code will run, and set the OnLostFocus and OnGotFocus properties of
controls on the form. These events will call the other function, which
actually does the highlighting and dehighlighting.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
I like your solution, but I am not sure how to use it.
I put your code into a module, but I do not know what to put on the form
to
activate it.
Anne

Allen Browne said:
Yes, you need to put this in the events of each control, but you can do that
programmatically.

The following example opens the form in design view, and sets the OnGotFocus
and OnLostFocus properties of all the text boxes and combo boxes:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function


You have to put this in every field? Isn't there one you can use on
open
of
the form?
Anne


Everyone is asking me to show the
field which has focus in a different
color. I am been applying conditional
formatting to each individual field.
Is there a way to do this with code?

In the GotFocus event, put code:

Me!theFieldName.Backcolor = xxxyyyzzz

where xxxyyyzzz is a color number (find a color with the color
picker
on
some existing control, and then copy the number).

In the LostFocus event, put code to change the backcolor to the default.

Larry Linson
Microsoft Access MVP
 
I had sent the other reponse before I even tried it. But cannot get this to
work.
I pasted your code into new new module.
I pasted:
? SetupHightlight("FrmContractor") - FrmContractor is one of my forms.
with the question mark in front of it. But it does nothing when click
through the form.
Anne



Allen Browne said:
1. Select the module tab of the Database window, and click New.
Access opens a new module.

2. Paste in the code.

3. Open the Immediate window: Ctrl+G

4. Enter:
? SetupHightlight("Form1")
substituting your actual form name for Form1.

The code will run, and set the OnLostFocus and OnGotFocus properties of
controls on the form. These events will call the other function, which
actually does the highlighting and dehighlighting.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
I like your solution, but I am not sure how to use it.
I put your code into a module, but I do not know what to put on the form
to
activate it.
Anne

Allen Browne said:
Yes, you need to put this in the events of each control, but you can do that
programmatically.

The following example opens the form in design view, and sets the OnGotFocus
and OnLostFocus properties of all the text boxes and combo boxes:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function


You have to put this in every field? Isn't there one you can use on
open
of
the form?
Anne


Everyone is asking me to show the
field which has focus in a different
color. I am been applying conditional
formatting to each individual field.
Is there a way to do this with code?

In the GotFocus event, put code:

Me!theFieldName.Backcolor = xxxyyyzzz

where xxxyyyzzz is a color number (find a color with the color
picker
on
some existing control, and then copy the number).

In the LostFocus event, put code to change the backcolor to the default.

Larry Linson
Microsoft Access MVP
 
No. Just save the code in a module, and enter the line into the immediate
window naming each form you want to behave like this.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
Got it, but I need to use it for several forms. Do I create a new module
for
every form?
Anne

Allen Browne said:
1. Select the module tab of the Database window, and click New.
Access opens a new module.

2. Paste in the code.

3. Open the Immediate window: Ctrl+G

4. Enter:
? SetupHightlight("Form1")
substituting your actual form name for Form1.

The code will run, and set the OnLostFocus and OnGotFocus properties of
controls on the form. These events will call the other function, which
actually does the highlighting and dehighlighting.


Anne said:
I like your solution, but I am not sure how to use it.
I put your code into a module, but I do not know what to put on the
form
to
activate it.
Anne

Yes, you need to put this in the events of each control, but you can
do
that
programmatically.

The following example opens the form in design view, and sets the
OnGotFocus
and OnLostFocus properties of all the text boxes and combo boxes:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function
 
You pasted the code into the module?
Then entered the line starting with the question mark into the Immediate
Window? And pressed Enter?

Did you receive an error?
Did you save the form, and test it?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
I had sent the other reponse before I even tried it. But cannot get this to
work.
I pasted your code into new new module.
I pasted:
? SetupHightlight("FrmContractor") - FrmContractor is one of my forms.
with the question mark in front of it. But it does nothing when click
through the form.
Anne



Allen Browne said:
1. Select the module tab of the Database window, and click New.
Access opens a new module.

2. Paste in the code.

3. Open the Immediate window: Ctrl+G

4. Enter:
? SetupHightlight("Form1")
substituting your actual form name for Form1.

The code will run, and set the OnLostFocus and OnGotFocus properties of
controls on the form. These events will call the other function, which
actually does the highlighting and dehighlighting.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
I like your solution, but I am not sure how to use it.
I put your code into a module, but I do not know what to put on the
form
to
activate it.
Anne

Yes, you need to put this in the events of each control, but you can
do
that
programmatically.

The following example opens the form in design view, and sets the
OnGotFocus
and OnLostFocus properties of all the text boxes and combo boxes:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function
 
I did not hit enter before after entering into the immediate window:
Now I got compile error, ambiguous name detected: SetUpHightlight
I copied and pasted my codes, so you can see what I have:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function

Immediate window:
? SetupHightlight("FrmContractor")


Allen Browne said:
You pasted the code into the module?
Then entered the line starting with the question mark into the Immediate
Window? And pressed Enter?

Did you receive an error?
Did you save the form, and test it?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
I had sent the other reponse before I even tried it. But cannot get this to
work.
I pasted your code into new new module.
I pasted:
? SetupHightlight("FrmContractor") - FrmContractor is one of my forms.
with the question mark in front of it. But it does nothing when click
through the form.
Anne



Allen Browne said:
1. Select the module tab of the Database window, and click New.
Access opens a new module.

2. Paste in the code.

3. Open the Immediate window: Ctrl+G

4. Enter:
? SetupHightlight("Form1")
substituting your actual form name for Form1.

The code will run, and set the OnLostFocus and OnGotFocus properties of
controls on the form. These events will call the other function, which
actually does the highlighting and dehighlighting.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

I like your solution, but I am not sure how to use it.
I put your code into a module, but I do not know what to put on the
form
to
activate it.
Anne

Yes, you need to put this in the events of each control, but you can
do
that
programmatically.

The following example opens the form in design view, and sets the
OnGotFocus
and OnLostFocus properties of all the text boxes and combo boxes:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function
 
The "ambiguious name" error means that you already have function or sub with
that name, or you pasted the function twice, or you tried to use the same
name for the module itself.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
I did not hit enter before after entering into the immediate window:
Now I got compile error, ambiguous name detected: SetUpHightlight
I copied and pasted my codes, so you can see what I have:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function

Immediate window:
? SetupHightlight("FrmContractor")


Allen Browne said:
You pasted the code into the module?
Then entered the line starting with the question mark into the Immediate
Window? And pressed Enter?

Did you receive an error?
Did you save the form, and test it?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
I had sent the other reponse before I even tried it. But cannot get this to
work.
I pasted your code into new new module.
I pasted:
? SetupHightlight("FrmContractor") - FrmContractor is one of my forms.
with the question mark in front of it. But it does nothing when click
through the form.
Anne



1. Select the module tab of the Database window, and click New.
Access opens a new module.

2. Paste in the code.

3. Open the Immediate window: Ctrl+G

4. Enter:
? SetupHightlight("Form1")
substituting your actual form name for Form1.

The code will run, and set the OnLostFocus and OnGotFocus properties
of
controls on the form. These events will call the other function, which
actually does the highlighting and dehighlighting.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

I like your solution, but I am not sure how to use it.
I put your code into a module, but I do not know what to put on the
form
to
activate it.
Anne

Yes, you need to put this in the events of each control, but you
can
do
that
programmatically.

The following example opens the form in design view, and sets the
OnGotFocus
and OnLostFocus properties of all the text boxes and combo boxes:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "],
False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function
 
Yeah, after it did not work the first time, I tried a second module. I
deleted the second module and it is now working. My mistake orginally was
that I did not hit enter in the immediate window.
How can I use this for other forms as well?
Anne

Allen Browne said:
The "ambiguious name" error means that you already have function or sub with
that name, or you pasted the function twice, or you tried to use the same
name for the module itself.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Anne said:
I did not hit enter before after entering into the immediate window:
Now I got compile error, ambiguous name detected: SetUpHightlight
I copied and pasted my codes, so you can see what I have:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function

Immediate window:
? SetupHightlight("FrmContractor")


Allen Browne said:
You pasted the code into the module?
Then entered the line starting with the question mark into the Immediate
Window? And pressed Enter?

Did you receive an error?
Did you save the form, and test it?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

I had sent the other reponse before I even tried it. But cannot get
this
to
work.
I pasted your code into new new module.
I pasted:
? SetupHightlight("FrmContractor") - FrmContractor is one of my forms.
with the question mark in front of it. But it does nothing when click
through the form.
Anne



1. Select the module tab of the Database window, and click New.
Access opens a new module.

2. Paste in the code.

3. Open the Immediate window: Ctrl+G

4. Enter:
? SetupHightlight("Form1")
substituting your actual form name for Form1.

The code will run, and set the OnLostFocus and OnGotFocus properties
of
controls on the form. These events will call the other function, which
actually does the highlighting and dehighlighting.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

I like your solution, but I am not sure how to use it.
I put your code into a module, but I do not know what to put on the
form
to
activate it.
Anne

Yes, you need to put this in the events of each control, but you
can
do
that
programmatically.

The following example opens the form in design view, and sets the
OnGotFocus
and OnLostFocus properties of all the text boxes and combo boxes:

Function SetupHightlight(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "],
False)"
End Select
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bHighlighted As Boolean)
If bHighlighted Then
ctl.BackColor = vbYellow
Else
ctl.BackColor = vbWhite
End If
End Function
 
Back
Top