determine if a field is in focus

  • Thread starter Thread starter Phil Hellmuth
  • Start date Start date
P

Phil Hellmuth

In a form event, I want to determine if a particular field is in focus.
Any suggestions on how to do this?

Thanks in advance.
 
Phil Hellmuth said:
In a form event, I want to determine if a particular field is in
focus. Any suggestions on how to do this?

I'm not sure if by "is in focus", you mean "has the focus". If so, the
form has an ActiveControl property that returns an object reference to
the control that has the focus, or raises an error if no control on the
form has the focus. So in code behind a form, you could say

If Me.ActiveControl Is Me.txtSomeControl Then
' txtSomeControl has the focus
End If

or

If Me.ActiveControl.Name = "txtSomeControl" Then
' txtSomeControl has the focus
End If

Be aware that the form's ActiveControl property returns a reference to
the control that has the focus *on that form*, even if that form doesn't
currently have the focus. If you need to know which control has the
focus among all the forms and datasheets that may be open, you should
check Screen.ActiveControl instead.
 
Phil Hellmuth said:
In a form event, I want to determine if a particular field is in
focus. Any suggestions on how to do this?

Thanks in advance.

By the way, I notice you also posted this question independently in a
different newsgroup. That's called "multiposting", and it's generally
frowned on because others don't know what answers have already been
given, and so they duplicate the effort. Also it's harder for you to
keep track of the various replies, and it's harder for later readers of
the question, who may be looking for the same answer, to learn what they
need.

In most cases a single, well-chosen newsgroup will do. If your question
really is relevant to more than one newsgroup, the approved technique is
to "crosspost" it instead, by listing multiple newsgroups in the To: or
Newsgroups: line of a single message. If you do that, the message and
any replies will appear in all the listed newsgroups automatically,
which is beneficial to all concerned.
 
If Me.ActiveControl.Name = "particular control name" Then

If my control is named e.g. "btnMyButton", this is (within the form's
module) equivalent to:

If ActiveControl.Name = btnMyButton.Name Then

This works fine; thanks for the hint! Have you got an idea, why the
following does not work, though in my opinion it should:

If ActiveControl = btnMyButton Then

This was my first attempt, yielding error "object does not support this
property or method".

By the way: type casting btnMyButton from Access.Button to Access.Control
has not helped.

Thanks for every further hint!

JotKa
 
If ActiveControl = btnMyButton Then

No: this will compare the default properties of each object (and I don't
think that a command button has one?). This does what you want:

If ActiveControl Is btnMyButton

which compares the objects themselves.

HTH


Tim F
 
Back
Top