Highlight Field

  • Thread starter Thread starter Sean
  • Start date Start date
S

Sean

I need to meet some standards for database development at
my office and one of the things I am required to do is
to "draw the attention of the user to the field being
entered"

I am assuming there is a way to have a user tab to a field
in the form and have the field he/she tabs into become
highlighted (with color, black and white, bold, it does
not matter).

Is this possible and how do I do it?

Thanks for the help

Sean
 
try this

paste this on a module
' if the control gets the focus changes the color to yellow
Public Function CtlGotFocus()
On Error Resume Next
Dim ctl As Control
Set ctl = Screen.ActiveControl
ctl.BackColor = RGB(255, 255, 0)
End Function

' if the control loses the focus changes the color to back to white
Public Function CtlLostFocus()
On Error Resume Next
Dim ctl As Control
Set ctl = Screen.ActiveControl
ctl.BackColor = RGB(255, 255, 255)
End Function

paste this on your form
'sets the OnGotFucus and OnLostFocus to call the avobe functions for each
TextBox (hence 109 on the control type) That way you won't have to set this
on all your controls (real time saveer)., if you want to do it on all forms,
just paste this on all forms, or put it on a module (first modify it)it so
that it gets called everytime a forms is opened.

Private Sub Form_Open(Cancel As Integer)
On Error Resume Next
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = 109 Then
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
End If
Next
End Sub

Rodrigo.
 
Hallo,

Tried the below. Works fine!

How can i adapt this to work in a module so i can call i in the form_open

Tried

Public Function setHighlight()
On Error Resume Next
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = 109 Then
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
End If
Next
End Function

I get a error on the me.controls line. Any suggestions?

Thanx in advance.

Regards,
Harmannus
 
Hallo,

2 questions:

1. Is it possible to extend the code to highlight a combo box list? It works
on all other controls but not on combo box list...
2. The code your provide doesn't work in a module. It needs to be on the
active form. Any suggestion on how to put it in a module as you suggested...

Regards,
Harmannus
 
Hallo,

To top the below code you could ad the below code to e.g. your start-up
form.

Application.SetOption "Behavior Entering Field", 1

This lets the cursor start at the beginning of the field instead of
selecting the whole field. Like this the below code doesn't conflict with
the default access behaviour.


Regards,

Harmannnus

.... keep in mind that the above line effects any Access database on your
system.
 
Hi,

change this:
Private Sub Form_Open(Cancel As Integer)
On Error Resume Next
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = 109 Then
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
End If
Next
End Sub

to
Private Sub Form_Open(Cancel As Integer)
On Error Resume Next
call handleopenform(me)
End Sub

And paste this on a module. It does the same thing, but now the only thing
you have to put on your forms is call the handleopenform(me) everytime they
open (when you want to change the colors that is)

public function HandleOpenForm(frm as form)
on error resume next
dim ctl as control
for each ctl in frm.controls
select case ctl.controltype
case acTextBox
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
case acComboBox '
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
case else
' do nothing
end select
next ctl

End function



Rodrigo.
 
for the first answer check my prior post.
for the seccond.

this part:
Set ctl = Screen.ActiveControl

sets the control to the active control on the current form. If you don't
have a form open with the focus on a control then it wont work.

To have it work without the Screen.ActiveControl you can change the got/lost
focus functions to receive the name of the controls (which you can also
dinamically set with the open form functions) and then just set the ctl to
that control. For this to work, you'll need to receive both the form name
and the cotrol name:

Public Function ThiscltGotFocus(frmName as string, ctlName as string)
dim frm as form
dim ctl as control
set frm = forms(frmname)
set ctl = frm.controls(cltname)
ctl.BackColor = RGB(255, 255, 0)
set clt = nothing
set frm = nothing
end function

do the same to the other module, and change the open form to this, and
hopefully everyhing will work.
Private Sub Form_Open(Cancel As Integer)
On Error Resume Next
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = 109 Then
ctl.OnGotFocus = "=CtlGotFocus(" & me.name & ", " & clt.name & ")"
ctl.OnLostFocus = "=CtlLostFocus(" & me.name & ", " & clt.name & ")"
End If
Next
End Sub

keep in mind i wrote this here, and there could be some typos.
Rodrigo.
 
Hallo Rodrigo,

if i put the below code in "modHighlight" and Call HandleOpenForm(Me) in the
open_form event of a form i get a error on the form stating the ctr is not
active. Thanx in advance for your help!


' if the control gets the focus changes the color to yellow
Function CtlGotFocus()
On Error Resume Next
Dim ctl As Control
Set ctl = Screen.ActiveControl
ctl.BackColor = RGB(255, 255, 0)
End Function

' if the control loses the focus changes the color to back to white
Function CtlLostFocus()
On Error Resume Next
Dim ctl As Control
Set ctl = Screen.ActiveControl
ctl.BackColor = RGB(255, 255, 255)
End Function

Function HandleOpenForm(frm As Form)
On Error Resume Next
Dim ctl As Control
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
Case acComboBox '
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
Case Else
' do nothing
End Select
Next ctl
End Function

Regards,
Harmannus
 
Hallo Rodrigo,

Again, adapted your suggestion in a earlier post to "modHighlight". Still a
no go...

' if the control gets the focus changes the color to yellow
Function CtlGotFocus(frmName As String, ctlName As String)
Dim frm As Form
Dim ctl As Control
Set frm = Forms(frmName)
Set ctl = frm.Controls(ctlName)
ctl.BackColor = RGB(255, 255, 0)
Set ctl = Nothing
Set frm = Nothing
End Function

' if the control loses the focus changes the color to back to white
Function CtlLostFocus(frmName As String, ctlName As String)
Dim frm As Form
Dim ctl As Control
Set frm = Forms(frmName)
Set ctl = frm.Controls(ctlName)
ctl.BackColor = RGB(255, 255, 255)
Set ctl = Nothing
Set frm = Nothing
End Function

Function HandleOpenForm(frm As Form)
On Error Resume Next
Dim ctl As Control
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
Case acComboBox '
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
Case Else
' do nothing
End Select
Next ctl
End Function
 
Ups I missed the () on the got/lost focus

ctl.OnGotFocus = "=CtlGotFocus()"
ctl.OnLostFocus = "=CtlLostFocus()"
Case acComboBox '
ctl.OnGotFocus = "=CtlGotFocus()"
ctl.OnLostFocus = "=CtlLostFocus()"

Rodrigo
 
Hallo,

I my "modHighlight" i have the below code. A Call HandleOpenForm(Me) on a
one of my forms but nothing happens. No errors but also no highlights....

Any last tips?

Thanx for the assistance sofor!

Regards,
Harmannus


----



Option Compare Database

' if the control gets the focus changes the color to yellow
Function CtlGotFocus(frmName As String, ctlName As String)
Dim frm As Form
Dim ctl As Control
Set frm = Forms(frmName)
Set ctl = frm.Controls(ctlName)
ctl.BackColor = RGB(255, 255, 0)
Set ctl = Nothing
Set frm = Nothing
End Function

' if the control loses the focus changes the color to back to white
Function CtlLostFocus(frmName As String, ctlName As String)
Dim frm As Form
Dim ctl As Control
Set frm = Forms(frmName)
Set ctl = frm.Controls(ctlName)
ctl.BackColor = RGB(255, 255, 255)
Set ctl = Nothing
Set frm = Nothing
End Function

Function HandleOpenForm(frm As Form)
On Error Resume Next
Dim ctl As Control
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox
ctl.OnGotFocus = "=CtlGotFocus()"
ctl.OnLostFocus = "=CtlLostFocus()"
Case acComboBox '
ctl.OnGotFocus = "=CtlGotFocus()"
ctl.OnLostFocus = "=CtlLostFocus()"
Case Else
' do nothing
End Select
Next ctl
End Function
 
you forgot to add the name of the form and control to the handleopenform
function. Remember the bot/lost focus functions need them to work.

Try this

Function HandleOpenForm(frm As Form)
On Error Resume Next
Dim ctl As Control
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox
ctl.OnGotFocus = "=CtlGotFocus(" & frm.name & ", " & clt.name &
")"
ctl.OnLostFocus = "=CtlLostFocus(" & frm.name & ", " & clt.name
& ")"
Case acComboBox '
ctl.OnGotFocus = "=CtlGotFocus(" & frm.name & ", " & clt.name &
")"
ctl.OnLostFocus = "=CtlLostFocus(" & frm.name & ", " & clt.name
& ")"
Case Else
' do nothing
End Select
Next ctl
End Function

Rodrigo.
 
Hallo Rodrigo,

Now i have the below. But still nothing hapens. No erros but also no
higlights. Tried various combinations but do not see where it breaks up.
Hope you still have the patience to help me. Thanx for the assistance so
for. I realy like this. Would be nice to get it to work....

Regards,
Harmannus



Option Compare Database
' if the control gets the focus changes the color to yellow
Function CtlGotFocus(frmName As String, ctlName As String)
Dim frm As Form
Dim ctl As Control
Set frm = Forms(frmName)
Set ctl = frm.Controls(ctlName)
ctl.BackColor = RGB(255, 255, 0)
Set ctl = Nothing
Set frm = Nothing
End Function

' if the control loses the focus changes the color to back to white
Function CtlLostFocus(frmName As String, ctlName As String)
Dim frm As Form
Dim ctl As Control
Set frm = Forms(frmName)
Set ctl = frm.Controls(ctlName)
ctl.BackColor = RGB(255, 255, 255)
Set ctl = Nothing
Set frm = Nothing
End Function

Function HandleOpenForm(frm As Form)
On Error Resume Next
Dim ctl As Control
For Each ctl In Screen.ActiveForm.Controls
If TypeOf ctl Is TextBox Or TypeOf ctl Is ComboBox Then
ctl.OnGotFocus = "=CtlGotFocus(" & frm.Name & ", " & clt.Name &
")"
ctl.OnLostFocus = "=CtlLostFocus(" & frm.Name & ", " & clt.Name
& ")"
End If
Next
End Function
 
those damned quotes. Its missing the quote before and after the form/control
name.

Try this:

Function HandleOpenForm(frm As Form)
On Error Resume Next
Dim ctl As Control
For Each ctl In Screen.ActiveForm.Controls
If TypeOf ctl Is TextBox Or TypeOf ctl Is ComboBox Then
ctl.OnGotFocus = "=CtlGotFocus('" & frm.Name & "', '" &
clt.Name & "')"
ctl.OnLostFocus = "=CtlLostFocus('" & frm.Name & "', '" &
clt.Name & "')"
End If
Next
End Function

it should have been OnCotFocus = ctlGotFocus("formname","controlname")
and it was doing OnGotFocus = ctlGotFocus(formname,controlname)

Rodrigo.
 
Also if you are gonna use
For Each ctl In Screen.ActiveForm.Controls
you don't need to pass the frm on your function Function HandleOpenForm(frm
As Form) you can just change it to
Function HandleOpenForm()
and change the open form to:
call handleopenform

however, if you have multiple forms opened, it better if you change it to
For Each ctl In frm.Controls

that way it always knows which form is calling it.

Rodrigo.
 
Hallo Rodrigo,

Thanx for your efford! Learning a lot here!

Tried your suggestion in the open_form "call HandleOpenForm" but still no
highligts. Tried addapting your code to the best of my knowledge but can't
figure out why it will not work. My situation is that i use a search form on
top of a main form. So multiple forms open.

modHighlights:

' if the control gets the focus changes the color to yellow
Function CtlGotFocus(frmName As String, ctlName As String)
Dim frm As Form
Dim ctl As Control
Set frm = Forms(frmName)
Set ctl = frm.Controls(ctlName)
ctl.BackColor = RGB(255, 255, 0)
Set ctl = Nothing
Set frm = Nothing
End Function

' if the control loses the focus changes the color to back to white
Function CtlLostFocus(frmName As String, ctlName As String)
Dim frm As Form
Dim ctl As Control
Set frm = Forms(frmName)
Set ctl = frm.Controls(ctlName)
ctl.BackColor = RGB(255, 255, 255)
Set ctl = Nothing
Set frm = Nothing
End Function

Function HandleOpenForm()
On Error Resume Next
Dim ctl As Control
For Each ctl In frm.Controls
If TypeOf ctl Is TextBox Or TypeOf ctl Is ComboBox Then
ctl.OnGotFocus = "=CtlGotFocus('" & frm.Name & "', '" & clt.Name
& "')"
ctl.OnLostFocus = "=CtlLostFocus('" & frm.Name & "', '" &
clt.Name & "')"
End If
Next
End Function


Regards,
Harmannus
 
what you have to do is pick what you want to do.
its either

Function HandleOpenForm(frm as form)
On Error Resume Next
Dim ctl As Control
For Each ctl In frm.Controls

and you have to have this on the open form
call handleopenform(me)


or
Function HandleOpenForm()
On Error Resume Next
Dim ctl As Control
For Each ctl In screen.activeform.Controls

and just have this on the open form
call handleopenform

Rodrigo.
rodrigo.
 
Hallo Rodrigo,

Got it!

I typo in ctl.Name. It was clt.Name ;-)

Thanx for your patience!

Greatly appriciate it.

Now its time to go to sleep. It's 1:15 past midnight overhere in the
Netherlands ;-)

Regards,
Harmannus
 
Yes, there is a way to highlight the fields as you tab...
Place form in design view and hold down the shift key,
then click all the fields that you wish to highlight.
Then, go to "format" on menu bar and choose "conditional
formatting," on the drop down, choose "field has focus"
then select the colors you want at the top, you will see
the preview affects immediately. Once you select the field
background color you desire, click ok. Change to form
view, then tab fields to see color change. If this don't
work, then redo process. Don't give up@! Figure it out.

See ya!
 
Hallo Rodrigo,

The code doesn't work on a continous form (e.g. subform as continous form).
Tried addapting the code but cannot find a solution.

Any suggestions on that?

Thanx in advance!

Regards,
Harmannus
 
Back
Top