Me.Name is name of form. Is there a generic way to determine the nameof a control?

M

MikeB

If I code "Me.Name" in a VBA module, I can get the name of the form.
Is there a generic way that I can write some code to interrogate
something to get the name of the current control? I guess that would
be the control that has focus? So perhaps a collection of controls on
the form and then then iterate through those until I get one that has
"HasFocus = True?" and then what?

I want to write an event handler for a control and I want it to
pass the name of the control to a function or perhaps have the
function figure out the name of the control and the name of the event
handler that called it?

Reason is I'm writing a test form with a few controls to figure out
when each event is invoked and I'm doing a bunch of repetitive coding
that I'm looking to shortcut.

Thanks.
 
L

Larry Linson

Open a module window, and search for Screen.ActiveControl in VBA Help.

Larry Linson
Microsoft Office Access MVP
 
J

John Spencer

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)

All of those properties will raise an error if there is no such object.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 
A

Allen Browne

Each form has an active control, so use:
Me.ActiveControl.Name

Be sure to use error handling, in case there is no active control. For
example, if you click on the Record Selector, no control has focus and you
get error 2474.
 
M

MikeB

Each form has an active control, so use:
Me.ActiveControl.Name

Be sure to use error handling, in case there is no active control. For
example, if you click on the Record Selector, no control has focus and you
get error 2474.

Larry, John and Allen,
Thanks for the replies. Here is how I coded it.

On a side note, I tried coding the following, but it didn't work. I
assume this is because a) I don't understand VBA (and/or OO coding)
or b) VBA isn't fully object-oriented?

strControlName = Screen.ActiveControl.Name

The working code is:

Sub send_msg(ByVal msg As String)
Dim strControlName As String
Dim strFormname As String
strControlName = " "
strFormname = Me.Name
On Error GoTo Err_send_msg
Dim ctlCurrentControl As Control
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name
Err_send_msg:
MsgBox "From Form: " & strFormname & vbCrLf & _
" Control: " & strControlName & vbCrLf & _
" Message: " & msg, _
vbOKOnly, "Event Tracer"
End Sub
 
L

Larry Linson

I put the following in the Click event of a cmdButton (originally used for
another purpose), and it worked for me to show the name of that control in
the MsgBox:

MsgBox Screen.ActiveControl.Name, vbOKOnly, "Test Screen ActiveControl"

So did the following:

Dim CName As String
CName = Screen.ActiveControl.Name
MsgBox CName, vbOKOnly, "Test Screen ActiveControl"

So did creating a function in a standard module:

Function WhatControlName(ctl As Control) As String
WhatControlName = ctl.Name
End Function

and calling it with

MsgBox WhatControlName(Screen.ActiveControl), vbOKOnly, "Test Screen
ActiveControl"

I'm not sure what difficulty you encountered, but it seems pretty flexible.
There _does_ have to be an active Form or Report, and some Control does have
to be active (have the focus).

And, as an aside, no, VBA is not "fully object oriented" -- it lacks
"inheritance". But, except for recent generations of programmers who were
trained that "if it's not fully OO, it's not development", that has been no
barrier to developers some very significant applications being created which
use VBA.

And, if you were thinking that it has nothing to do with the question you
posed, you are 100% correct.

Larry Linson
Microsoft Office Access MVP
 

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