What has focus?

  • Thread starter Thread starter Doc
  • Start date Start date
D

Doc

This should be easy! I'm looking for a command that would return the name or
object type that has the current focus. Something to the affect of

Msgbox CurrentProject.HasFocus

But obviously that doesn't work. . . I need to know it it's a form, table,
query, or report.

Thanks!
 
Doc said:
This should be easy! I'm looking for a command that would return the
name or object type that has the current focus. Something to the
affect of

Msgbox CurrentProject.HasFocus

But obviously that doesn't work. . . I need to know it it's a form,
table, query, or report.

Thanks!

Screen.ActiveControl
 
Doc said:
This should be easy! I'm looking for a command that would return the name
or
object type that has the current focus. Something to the affect of

Msgbox CurrentProject.HasFocus

But obviously that doesn't work. . . I need to know it it's a form,
table,
query, or report.


There are several ways to approach this. There are some properties of the
Screen object that return object references to active objects:

+ Screen.ActiveControl - returns a reference to the control that has the
focus (if there is one)

+ Screen.ActiveForm - returns a reference to the form that has the focus
(if there is one and it's not a popup form)

+ Screen.ActiveReport - returns a reference to the report that has the
focus (if there is one and it's not a popup report)

+ Screen.ActiveDatasheet - returns a reference to the table or query
datasheet that has the focus (if there is one)

All of those properties will raise an error if there is no such object.
There's a difficulty with the ActiveDatasheet property, in that it's not
easy to tell whether the datasheet is a query datasheet or a table datasheet
(though you can figure it out from the TypeName of the object). Also,
ActiveDatasheet won't tell you about a query or table that is open in design
view.

There are also a couple of properties of the Application object that can
come in handy. I'm not sure when they were introduced, but they exist in
Access 2003, at least. These are

+ Application.CurrentObjectName - the name of the current object,
whether it's open or not, even if it's just selected in the database window

+ Application.CurrentObjectType - the type (numeric, from the
AcObjectType enumeration; e.g., acTable = 0, acQuery = 1, acForm = 2, etc.)
of the current object.

I haven't played with these properties much, but they look much more useful
than the Screen.ActiveXXXXX properties. however, they don't tell you
whether the current object is open, or what view it's in. For that, you can
use SysCmd acSysCmdGetObjectState, or check the IsLoaded property of the
appropriate AccessObject via CurrentProject.AllXXXXX or
CurrentData.AllXXXXX.
 
Back
Top