Control Focus

M

Monty

How can I programmatically detect is a particular control has focus?
Is there something like a "HasFocus" property I can test?

Thanks
 
D

Dirk Goldgar

Monty said:
How can I programmatically detect is a particular control has focus?
Is there something like a "HasFocus" property I can test?

There's no HasFocus property, but the form has an ActiveControl property
that returns an object reference to the control on the form (if any)
that currently has the focus. So you could write a function like this:

'----- start of code -----
Function HasFocus( _
ctl As Access.Control, _
frm As Access.Form) _
As Boolean

On Error Resume Next
If ctl Is frm.ActiveControl Then
HasFocus = True
End If

End Function

'----- end of code -----

And use it like this:

If HasFocus(Me.Text1, Me) Then
' Text1 has the focus.
End If
 
F

fredg

How can I programmatically detect is a particular control has focus?
Is there something like a "HasFocus" property I can test?

Thanks

Why you wish to do this would have been helpful.
As it is, code a command button click event:
MsgBox IIf(Screen.Previouscontrol = "WhatControl","Yes What Control
has focus","No " & Screen.PreviousControl.Name & " had focus"))
 
M

Monty

Dirk said:
There's no HasFocus property, but the form has an ActiveControl property
that returns an object reference to the control on the form (if any)
that currently has the focus. So you could write a function like this:

'----- start of code -----
Function HasFocus( _
ctl As Access.Control, _
frm As Access.Form) _
As Boolean

On Error Resume Next
If ctl Is frm.ActiveControl Then
HasFocus = True
End If

End Function

'----- end of code -----

And use it like this:

If HasFocus(Me.Text1, Me) Then
' Text1 has the focus.
End If

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Thanks, that is exactly what I asked for. But I realize I asked the
wrong question once I looked over your procedure. I've posted the real
question as another topic.
 
M

Monty

fredg said:
Why you wish to do this would have been helpful.
As it is, code a command button click event:
MsgBox IIf(Screen.Previouscontrol = "WhatControl","Yes What Control
has focus","No " & Screen.PreviousControl.Name & " had focus"))

I realized my question wasn't very complete, or even accurate, when I
read the first response. I rephrased my question and submitted it as a
new post "Soliciting procedural advice".

Thanks
 

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