0

In my form, I need to detect if focus is moved to a new control.

Can I do this for the entire form without having to create event procedures for every control?

I've tried this, but it doesn't seem to fire.

Private Sub Form_SelectionChange()
    MsgBox Screen.ActiveControl.Name
End Sub
bgmCoder
  • 6,205
  • 8
  • 58
  • 105

3 Answers3

0

Yes, use WithEvents. It takes a little and documentation is poor, but it may pay off in the end:

Using Dynamic External Event Procedures

Gustav
  • 53,498
  • 7
  • 29
  • 55
  • Wow, that is a change of approach. I like it - thanks for the reference. Now I just have to digest it. – bgmCoder Jul 03 '15 at 21:45
  • Can I do this alongside regular click events on the controls? – bgmCoder Jul 03 '15 at 21:53
  • This will let me create events for controls that don't already use any events; but how do I do both? – bgmCoder Jul 03 '15 at 22:22
  • Just do it. Your class modul with the WithEvents code is referenced when loading and unloading the form. You can reference more than one class - that's the smart part - so you can add just that functionality for a given form you may require. These will work alongside any "normal" code-behind-form you may have or may add. – Gustav Jul 04 '15 at 06:57
0

This creates an event on each control (not exactly what you asked for) but it is pretty clean.

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each control As Control In Controls
        AddHandler control.Enter, AddressOf ControlReceivedFocus
    Next
End Sub

Private Sub ControlReceivedFocus(sender As Object, e As EventArgs)
    MessageBox.Show(sender.ToString())
End Sub

End Class

source: https://stackoverflow.com/a/1159481/2352507

Community
  • 1
  • 1
SunsetQuest
  • 8,041
  • 2
  • 47
  • 42
  • That's an interesting way of doing it. I could filter out some of the unnecessary controls in the for loop. Thanks for the quick answer. – bgmCoder Jul 03 '15 at 21:26
  • Sorry to say, but you can't do this in VBA it seems. There isn't any `addhandler`, firstly, and you have to use a reference to the parent form in the `for each` statement. See this thread: https://social.msdn.microsoft.com/Forums/en-US/4cae16a1-9c74-4e2b-bd69-24edf24322cd/is-there-a-vba-for-excel-equivalent-to-addhandler-from-vb?forum=isvvba – bgmCoder Jul 03 '15 at 22:18
  • That's VB.NET. You need VBA. – Gustav Jul 04 '15 at 06:40
0

This thread has a solution to add onClick events to controls dynamically. As it turns out, although you can add your own event, you can't add your own event AND have your normal control_click() event, too. It's just that you can set it at runtime via code, and it can be named whatever you want (as long as it is a function).

This sort of answers my question: you CAN add an onclick event to every control dynamically without having to create them in the gui. Here is the syntax which you can put in form_onload if you like:

with Me.myControl
    Me.myControl.OnEnter = "=SomeFunction()"
end with

And here's how to add an .OnEnter event to every textbox control (just call the SetReportControls method from form_open:

Private Sub SetReportControls()
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        With ctrl
            If TypeOf ctrl Is Access.TextBox Then
                .OnEnter = "=ReportControl(false)"
            End If
        End With
    Next
End Sub

Private Function ReportControl(Cancel As Integer)
    msgbox Screen.ActiveControl.Name
End Function
Community
  • 1
  • 1
bgmCoder
  • 6,205
  • 8
  • 58
  • 105
  • Don't do this. It's a left-over from Access 1.x where you didn't have the option for code-behind-form modules. It may work, but you loose error handling and - even worse - it is a nightmare to maintain as you cannot easily search or browse what functions ar called where. – Gustav Jul 04 '15 at 07:00
  • Actually, when you view the controls in the GUI's properties, you can see that the function is attached. You might be right about the error handling, though. – bgmCoder Jul 04 '15 at 13:10
  • Yes, you can see one control at a time. But this is very slow process. – Gustav Jul 04 '15 at 21:01