How to refer to "current control" in event handler

  • Thread starter Thread starter Gary Schuldt
  • Start date Start date
G

Gary Schuldt

I don't believe I've seen a shorthand way to refer to "the control whose
event triggered this CBF". I'm aware of the use of Me to refer to the
current form.

For example, if I double-click a MyTextControl and start it's double-click
event procedure, is there a way (other than Me.MyTextControl) to refer to
the control?

Thanks.

Gary
 
Hi Gary.

I wish there was a way to determine this, but VBA does not expose it.

If the event was triggered by the user (not called by another piece of
code), it might be Screen.ActiveControl.
 
Hi, Mark,

So, if I wanted to examine the value of the active control, the reference
would be:

Me.ActiveControl.Value?

Gary
 
Thanks, Allen,

OK, so then I have a style question:

In your own coding, do you use the ActiveControl reference, or do you
explicitly state the fully qualified name of the control when you can assume
that the event is a user-triggered one?

For example, on update of an option box MyOptionBox, would you code:

Select Case Me.ActiveControl.Value '(is Value the correct coding here?)
Case 1 . . .
Case 2 . . .
etc.
End Select

or would you code the reference Me.MyOptionBox?

Thanks,

Gary
 
Hi, Maurice,

I was looking for a general reference I could use in ALL event handlers,
regardless of the actual control name.

For example, at the form level, I don't have to type the name of the active
form, I can always rely on a "Me" reference. I was looking for the
counterpart at the control level.

It looks like ActiveControl is probably what I want (see other replies).

Thanks.

Gary

StCyrM said:
Hi Gary

You can also use the following:

[forms]![FormName]![MyTextControl]

Maurice

I don't believe I've seen a shorthand way to refer to "the control whose
event triggered this CBF". I'm aware of the use of Me to refer to the
current form.

For example, if I double-click a MyTextControl and start it's double-click
event procedure, is there a way (other than Me.MyTextControl) to refer to
the control?

Thanks.

Gary
 
Personally, I never use Screen.ActiveControl, as I don't find it reliable
enough. It's kinda like SendKeys, where you hope the right thing has the
focus at the time your code runs, and I do (not infrequently) call one
routine from another.

So, yes, I hard-code the name of the control. Even if I am calling a generic
routine in a standard module, I will pass in the control that's being
processed rather than rely on Screen.ActiveControl.

Simple example: client had a form with 184 controls on it, and wanted the
active one yellow. Could probably have used Screen.ActiveControl for that,
but we preferred to programmatically set the OnGotFocus and OnLostFocus so
that each text box/combo box identified itself by name:

Public Function MakeHighlight(strFormName As String) As Boolean
Dim ctl As Control

DoCmd.OpenForm strFormName, acDesign

For Each ctl In Forms(strFormName).Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
ctl.OnGotFocus = "=Highlight([" & ctl.Name & "],True)"
ctl.OnLostFocus = "=Highlight([" & ctl.Name & "],False)"
End If
Next

Set ctl = Nothing
End Function

Public Function Highlight(ctl As Control, bShow As Boolean)
ctl.BackColor = IIf(bShow, vbYellow, vbWhite)
End Function
 
Thanks much, Allen.

I won't be using ActiveControl, then, based on your advice. Your simile
with SendKeys really clinched it: I just got burned yesterday when I went
to use an application that worked on my desktop to a laptop ("field test")
and have it not work! (I was using SendKeys to preset the options in a Find
popup, and the window didn't at all display what I wanted it to.) I guess
I'll have to revisit the issue and figure out how to program it in VB.

I enjoyed reading your highlighting code as well. It's great to watch an
expert "in action"! Part of the logic actually is helping me with another
problem I'm working on.

(BTW, I spent a day on Rottnest Island a few years ago.)

Gary

Allen Browne said:
Personally, I never use Screen.ActiveControl, as I don't find it reliable
enough. It's kinda like SendKeys, where you hope the right thing has the
focus at the time your code runs, and I do (not infrequently) call one
routine from another.

So, yes, I hard-code the name of the control. Even if I am calling a generic
routine in a standard module, I will pass in the control that's being
processed rather than rely on Screen.ActiveControl.

Simple example: client had a form with 184 controls on it, and wanted the
active one yellow. Could probably have used Screen.ActiveControl for that,
but we preferred to programmatically set the OnGotFocus and OnLostFocus so
that each text box/combo box identified itself by name:

Public Function MakeHighlight(strFormName As String) As Boolean
Dim ctl As Control

DoCmd.OpenForm strFormName, acDesign

For Each ctl In Forms(strFormName).Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
ctl.OnGotFocus = "=Highlight([" & ctl.Name & "],True)"
ctl.OnLostFocus = "=Highlight([" & ctl.Name & "],False)"
End If
Next

Set ctl = Nothing
End Function

Public Function Highlight(ctl As Control, bShow As Boolean)
ctl.BackColor = IIf(bShow, vbYellow, vbWhite)
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Gary Schuldt said:
Thanks, Allen,

OK, so then I have a style question:

In your own coding, do you use the ActiveControl reference, or do you
explicitly state the fully qualified name of the control when you can assume
that the event is a user-triggered one?

For example, on update of an option box MyOptionBox, would you code:

Select Case Me.ActiveControl.Value '(is Value the correct coding here?)
Case 1 . . .
Case 2 . . .
etc.
End Select

or would you code the reference Me.MyOptionBox?

Thanks,

Gary
refer
to
 
Gary,

The value propery does not exist for the ActiveControl. You would have to
use a Control Object variable:

Dim ctlCurrent As Control
Debug.Print Me.ActiveControl.Name
Set ctlCurrent = Me.ActiveControl
If ctlCurrent.ControlType = acTextBox Then
Debug.Print ctlCurrent.Value
Debug.Print Me.ActiveControl
End If

Cheers
Mark
 
Albert,

thanks much for your extensive reply. I appreciate what you're saying about
Grabbing, as well as the fact that you've never had it fail. I can't help
wonder, however, if there is an underlying reason why Grabbing will always
work . . . or are the odds just much better? I have noticed the Grabbing
approach in much of the code I have read.

Thanks for the examples, too, as well as the custom menu information.

Gary
 
Back
Top