Re: How to refer to "current control" in event handler

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Gary Schuldt (garyschuldt_at_comcast.net)
Date: 03/09/04


Date: Mon, 8 Mar 2004 22:04:53 -0800

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" <AllenBrowne@SeeSig.Invalid> wrote in message
news:404d20ad$0$22531$5a62ac22@freenews.iinet.net.au...
> 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.
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
> "Gary Schuldt" <garyschuldt@comcast.net> wrote in message
> news:ecQo8OTBEHA.3568@tk2msftngp13.phx.gbl...
> > 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
> > "Allen Browne" <AllenBrowne@SeeSig.Invalid> wrote in message
> > news:404c9474$0$22516$5a62ac22@freenews.iinet.net.au...
> > > 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.
> > >
> > > "Gary Schuldt" <garyschuldt@comcast.net> wrote in message
> > > news:eh4297RBEHA.2888@TK2MSFTNGP09.phx.gbl...
> > > > 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
>
>



Relevant Pages

  • Re: User scroll dates question
    ... Make a Public Function for this: ... Function ChangeDate(KeyAscii As Integer, ActiveControl As Control) As ... you'd use the function in any KeyPress event like this: ...
    (microsoft.public.access.formscoding)
  • Re: Detect if dialog page is active and button enabled
    ... very risky way to control another program. ... then rather than sendkeys I would suggest ... Appleman doesn't talk about the tab control. ... window, by sending a WM_NOTIFY message ...
    (microsoft.public.vb.winapi)
  • RE: Passing form values to module
    ... Do I put this Function in my form code? ... Public Function GetControlText(ByRef ctl As Control) As String ... Public Function GetFormRecordsourceAs String ...
    (microsoft.public.access.modulesdaovba)
  • RE: Passing form values to module
    ... Public Function GetControlText(ByRef ctl As Control) As String ... Public Function GetFormRecordsourceAs String ... strCallingProc As String, strFieldName As String, strTableName As String) ...
    (microsoft.public.access.modulesdaovba)
  • Re: .NET / C# equivalent of GetFocus()?
    ... Control control = Control.FromHandle; ... instead the activecontrol of the form/usercontrol is ... > activecontrol of form can be things like the tab control, ... > directly on the form or your usercontrol. ...
    (microsoft.public.dotnet.framework.windowsforms.controls)