which control gets the focus?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I have an unbound form which I will use to collect all data about a
questionnaire.
There is a textbox for each question (about 40) where the user has to enter
values form 0 to 4.
I would like to use the status bar to give to the user some hint about each
specific question.
I have built a class for the questionnaire and I listed all the hints in a
procedure of the class module.
I will code a procedure on the form’s module that will use this list to get
the right hint, according on which control has been entered.
My problem is…since the user can enter whatever textbox in the form and he
couldn’t necessary go from the first to the last control in the right order,
I will need a way to track which control has been entered by the user and use
its name to show the appropriate hint in the status bar.
Obviously I don’t want to write lots of code in the form module, otherwise I
know I could write a procedure fired by the lostfocus event for each control.
But it sounds kind of waste of time…and memory.
Is there any form event that will allow me to know that a control has lost
the focus and another has get it?
I hope all this sounds clear.
 
You can write a generic function (must be a function, not a sub) that uses
the Screen.ActiveControl (and Screen.PreviousControl, if necessary) to
determine which control is active and takes the appropriate action.

Once you've written that function, select all of the text boxes for which
you want this function to apply. Once you have them all selected, go to the
Properties window and type =MyFunctionName() for the GotFocus or LostFocus
event. That'll mean your function will now be called when the event fires
for any of the currently selected controls.

I have to wonder, though, whether your database is properly normalized if
you have 40 separate textboxes for a questionnaire. You might want to take a
look at Duane Hookom's "At Your Survey" for an example of a properly
designed database
http://rogersaccesslibrary.com/Otherdownload.asp?SampleName='At Your Survey 2000'
 
Hi Rocco,

Try something like this, in the form's code module:

Public Function ShowHint

Dim Hint As String

Select Case Me.ActiveControl.Name
Case "X"
Hint = "Something"
Case "Y"
Hint = "Other"
...
End Select
'display hint on status bar or on a label on the form
...
End Function

Then set the On Enter event property of each textbox to
=ShowHint()

Instead of hard-coding the hints into a Select Case structure, it may be
worth storing them in a table and using DLookup() to retrieve the right
one each time.
 

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

Back
Top