Form active control change event

J

Jim Franklin

Hi,

Can anyone tell me if there is any way of running a bit of code every time
the focus moves from one control to another on a form? I know that I can use
the GotFocus and LostFocus events for each control individually, but I have
some forms with large numbers of controls and I want to run the same small
bit of code every time the focus changes, regardless of which control has
the focus. The controls themselves are not being updated.

Thanks for any help,

Jim
 
A

Allen Browne

Jim, what are you trying to achieve?

The example below shows you can highlight the active control in a form just
by setting the form's Load event. This calls code that sets the OnGotFocus
and OnLostFocus event of all the text boxes and combos on the form (unless
already set for some other purpose, or if set for transparent.)

To use it:
1. Paste the code below into a general module (so you can use it with any
form.)

2. Set the form's OnLoad property to exactly this:
=SetupHighlight([Form])
i.e. don't change the Form (don't use your form name.)

Public Function SetupHighlight(frm As Form)
Dim ctl As Access.Control
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
If ctl.BackStyle = 1 Then
If (ctl.OnGotFocus = vbNullString) And _
(ctl.OnLostFocus = vbNullString) Then
ctl.OnGotFocus = "=Hilight([" & ctl.Name & "], True)"
ctl.OnLostFocus = "=Hilight([" & ctl.Name & "], False)"
End If
End If
End Select
Next
End Function
Public Function Hilight(ctl As Control, bOn As Boolean)
Const lngcHilightOn = vbYellow
Const lngcHilightOff = vbWhite
If bOn Then
ctl.BackColor = lngcHilightOn
Else
ctl.BackColor = lngcHilightOff
End If
End Function
 
J

Jim Franklin

Hi Allen,

I can use this to get to where I want, as I don't think I will need a
separate GotFocus or LostFocus event for any single control. My app has to
reposition objects around the form, depending on which control has the
focus. The necessary information is stored in each controls Tag property, so
I just needed to run the same function every time the focus moved. I just
wondered if there was a way of always capturing changes in the
Screen.ActiveControl property. It would be useful sometimes if there was a
form event which fired as the focus moved between 2 controls on the form.

Thanks for your help, it's very much appreciated,

Jim


Allen Browne said:
Jim, what are you trying to achieve?

The example below shows you can highlight the active control in a form
just by setting the form's Load event. This calls code that sets the
OnGotFocus and OnLostFocus event of all the text boxes and combos on the
form (unless already set for some other purpose, or if set for
transparent.)

To use it:
1. Paste the code below into a general module (so you can use it with any
form.)

2. Set the form's OnLoad property to exactly this:
=SetupHighlight([Form])
i.e. don't change the Form (don't use your form name.)

Public Function SetupHighlight(frm As Form)
Dim ctl As Access.Control
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
If ctl.BackStyle = 1 Then
If (ctl.OnGotFocus = vbNullString) And _
(ctl.OnLostFocus = vbNullString) Then
ctl.OnGotFocus = "=Hilight([" & ctl.Name & "], True)"
ctl.OnLostFocus = "=Hilight([" & ctl.Name & "], False)"
End If
End If
End Select
Next
End Function
Public Function Hilight(ctl As Control, bOn As Boolean)
Const lngcHilightOn = vbYellow
Const lngcHilightOff = vbWhite
If bOn Then
ctl.BackColor = lngcHilightOn
Else
ctl.BackColor = lngcHilightOff
End If
End Function

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

Reply to group, rather than allenbrowne at mvps dot org.
Jim Franklin said:
Hi,

Can anyone tell me if there is any way of running a bit of code every
time the focus moves from one control to another on a form? I know that I
can use the GotFocus and LostFocus events for each control individually,
but I have some forms with large numbers of controls and I want to run
the same small bit of code every time the focus changes, regardless of
which control has the focus. The controls themselves are not being
updated.

Thanks for any help,

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top