Call a function every time I change focus


I have a simple script I run everytime a control gets focus.  This is on a datasheet.

Every time I move to a new control then the function is called.  Example: 20 controls means I need the same VBA function call 20 times.

QUESTION: How can I automatically call this funtion without need to call it for every single control individually.
Patrick O'DeaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John TsioumprisSoftware & Systems EngineerCommented:
If you want to repeat a function for a specific set of controls then the solution is event subclassing..
First you need a class module ..i call it SpecialEventHandler
Option Compare Database

Private WithEvents txtBox As TextBox
Private m_Form As Form
Private Const mstrEventProcedure = "[Event Procedure]"

Public Function init(txBox As TextBox, frm As Form)
  Set txtBox = txBox 
  Set m_Form = frm
  'Debug.Print frm.Name
  txtBox .SetFocus= mstrEventProcedure
End Function

Private Sub txtBox_SetFocus()

'   -------  Your code here-----
End Sub

Private Sub Class_Terminate()
    Set chkbx = Nothing
    Set m_Form = Nothing
End Sub

Open in new window

Then on the form you want to use event subclassing
Dim spEventHandler As SpecialEventHandler
Private coltextBoxes As New Collection

Open in new window

On the Open event
Private Sub Form_Open(Cancel As Integer)
  Dim ctl As Control

  CtlNames = vbNullString
  For Each ctl In Me.Detail.Controls
  Select Case ctl.ControlType
    Case acTextBox
' --- if it matches your criteria ----
       Set spEventHandler = New SpecialEventHandler
        spEventHandler.init Controls(ctl.NAME), Me
        coltextBoxes .Add spEventHandler
    End Select
  Next ctl
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Patrick O'DeaAuthor Commented:

I am just trying your code.
  Set spEventHandler = New SpecialEventHandler
        spEventHandler.init Controls(, Me
      **  coltextBoxes .Add spEventHandler
    End Select

Open in new window

The ** prefixed bit is gone "red".  I.e. the code is showing an error.
John TsioumprisSoftware & Systems EngineerCommented:
Have you placed the declaration of
Private coltextBoxes As New Collection

Open in new window

on the top of the form's code..
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

John TsioumprisSoftware & Systems EngineerCommented:
Just a clarification...the whole procedure is called "event sinking"...not "event subclassing"..its quite late and my memory plays games...
John TsioumprisSoftware & Systems EngineerCommented:
I guess this is the problem
coltextBoxes.Add spEventHandler

Open in new window

also this
txtBox.SetFocus= mstrEventProcedure

Open in new window

copy paste wasn't accurate
Patrick O'DeaAuthor Commented:

Thanks for your help.
(I am a little unsure of the nature of the VBA and where it goes)

See video explaining what I am trying to do.

Is there any chance that you could amend the attached frmHoldPrices to achieve what I need.

(please tell me if I am asking for inappropriate assistance).
Patrick O'DeaAuthor Commented:

Our letters crossed in the mail !

I will try again in the morning ... it's late here too!

Thanks again.
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I've used event sinking, and it's not a bad way to do things ... but if you're only doing this on a single form, it's not really worth the trouble to setup and troubleshoot. Instead, just copy the same code to the GotFocus event of those 20 controls and be done with it.
Patrick O'DeaAuthor Commented:
Thanks folks,

I was unable to get my code working but that's probably due to my own limitations.

All comments appreciated.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.