PDA

View Full Version : How can I perform a common action every time any control on a form gets the focus?



boukasa
11-11-2008, 06:14 PM
I have a form with multiple fields. When the user clicks in a field, I populate a "suggestions" list box that gives them common values for the field. The suggestions are looked up in a database table. They can select a suggestion and click a button to copy the suggestion to the last active text box control.

Each time the user moves from field to field, I need to tell the Suggestions list box to requery, to get suggestions for the current field. Is there a way to do this without writing a separate event procedure for every text box's OnGotFocus event? Is there some form event or other thing that happens in one place every time a new control on the form gets the focus?

(I cannot programmatically set the OnGotFocus events, because some text boxes have events written for their OnGotFocus event. The only solution I can think of is too complicated - to iterate every text box on the form, check if it has an OnGotFocus event, if so to store the event in an array, then later to call the stored event - basically simulating something like C++ function overriding - too much infrastructure!)

Thanks for any assistance.

CreganTur
11-12-2008, 06:38 AM
Welcome to the forum! Always good to have new members.

There are 2 options I can think of, you'll just need to decide which one is the best for you.

You could put your listbox update code in a module and call the module for the OnGotFocus event for all your textboxes- you'll just need to add in some validation so it only runs when the textbox is empty, for example.

The other option is you could build a custom class module to handle the listbox and your focus events. There's no way I could explain class modules in this post, but there are great resources out on the web.