Event when clicking any control on form

Galaxiom

Super Moderator
Staff member
Local time
Today, 13:01
Joined
Jan 20, 2009
Messages
12,887
Is there an event that fires when any control on the form is clicked? If not there should be.

The idea is that the same procedure would run regardless of which control is clicked and use the ActiveControl property to modify the behaviour of the Sub. This would save adding an event to each control and save a lot of work on a form with a lot of buttons, like a virtual keyboard.

Sure the coding could be added to the module with a loop but a click anything event would avoid that.

BTW Detail On Click doesn't do it in case you are wondering.
 
Is there an event that fires when any control on the form is clicked? If not there should be.

The idea is that the same procedure would run regardless of which control is clicked and use the ActiveControl property to modify the behaviour of the Sub. This would save adding an event to each control and save a lot of work on a form with a lot of buttons, like a virtual keyboard.

Sure the coding could be added to the module with a loop but a click anything event would avoid that.

BTW Detail On Click doesn't do it in case you are wondering.

That might cause a lot of extra wasted events firing when 75% or more of the controls are data entry control that would not need any event to fire.

You do not have to use an event procedure with VBA code behind the form.

I like to keep my forms as lite as possible by using little or no event procedures.

Enstead of one huge event for all controls, I create functions by tasks. I have the event call the desire function for that task.

Example:

This code would be in a standard module

Code:
'~~~~~~~~~~~~~~~~~~~~~~~~~~ RecordNew
Function RecordNew(Optional pF As Form _
   , Optional pFirstControl As Control) As Byte
   
   'example useage: OnClick event of a New Record command button
   ' = RecordNew()
   ' = RecordNew([Form])
   ' = RecordNew([Form], [Controlname])
   
   On Error GoTo Proc_Err
   
   If pF Is Nothing Then Set pF = Screen.ActiveForm
   
   'if there have been changes to the current record, save them
   If pF.Dirty Then pF.Dirty = False: DoEvents
   
   If Not pF.NewRecord Then
      If Not pF.AllowAdditions Then
         pF.AllowAdditions = True
         DoEvents
      End If
      pF.Recordset.AddNew
   End If
   
   If Not pFirstControl Is Nothing Then
      pFirstControl.SetFocus
   End If
   
   DoEvents
   
Proc_Exit:
   Exit Function
Proc_Err:
   If Err.Number = 2046 Then
      'You are already on a new record
      Exit Function
   End If
   MsgBox Err.Description, , _
     "ERROR " & Err.Number & "   RecordNewF"
 
   'press F8 to step through code and fix problem
   If IsAdmin() Then Stop: Resume
   Resume Proc_Exit

End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~ RecordDelete
Function RecordDelete(Optional pF As Form _
   , Optional pFirstControl As Control _
   , Optional pSkipMessage As Boolean = False) As Byte
   
   'example useage: OnClick event of a Delete Record command button
   ' = RecordDelete()
   ' = RecordDelete([Form])
   ' = RecordDelete([Form], [Controlname])
   
   On Error GoTo Proc_Err
   
   If MsgBox("Do you wish to permanently delete this record?" _
      , vbYesNo + vbDefaultButton2 _
      , "Delete Record?") = vbNo Then Exit Function
      
   If pF Is Nothing Then Set pF = Screen.ActiveForm
   
   If pF.Dirty Then pF.Dirty = False
   
   If Not pF.NewRecord Then
      pF.Recordset.Delete
   End If
   pF.Requery
   
   If Not pFirstControl Is Nothing Then
      pFirstControl.SetFocus
   End If

   Exit Function
Proc_Err:
   MsgBox Err.Number & " " & Err.Description, , "Cannot delete record"
End Function

You can then have an add record command button on any form by setting the On Click event to:

Code:
=RecordNew([Form],[MainName])
Note: This is not using the [event procedure].

This method allows all forms to share the same code for a specific task.

If I understand what you want, all controls on a single form must share the same event for unrelated tasks. So one event must handle all tasks for a single form.
 
Yes I wanted to keep the form as light as possible too and was using a similar strategy. It is for a form with something like fifty buttons that would all run the same procedure.

It is actually what I half expected Form_Click to do but it doesn't.

The only thing that triggers that event in a single form is the RecordSelector so it is clearly meant for records only and aught right be called Record_Click. :rolleyes:

Without On ClickAnything each button has to have a one line procedure call the sub (or function).

My dream was in the style of:

Sub Form_ClickAnything()
MyAction
End Sub

Sub MyAction()
Dim ButtoName as String
ButtonName = Screen.ActiveControl.Name
' Do stuff with Buttonname
End Sub

Which is about 49 times lighter than having to include a Sub to call MyAction for every individual button.;)

One might extend it to checking for a naming pattern or Tag to only call the other sub if it was matched to the group to allow the presence of other buttons.

Doesn't seem like such a bad feature.
Maybe I should suggest it for Access 2013. :rolleyes:
 
I still can wrap my head around a form with that many command buttons.

There may still be a way.

Maybe you could use a class to create a control array that has a common click event. I think I may have an example that does something like this. Would that be of interest to you?
 
I still can wrap my head around a form with that many command buttons.

It is a virtual keyboard one of our new members is working on. I got me thinking theoretically. Always dangerous when I go theoretical. ;)

http://www.access-programmers.co.uk/forums/showthread.php?p=967784#post967784

The Control Array Class is probably not the go for SweetBabe to take on just yet but I would be very interested in the example to further my own knowledge. I have only scratched the surface of Class and I would like a practical example to work through.
 
Yes I wanted to keep the form as light as possible too and was using a similar strategy. It is for a form with something like fifty buttons that would all run the same procedure.

It is actually what I half expected Form_Click to do but it doesn't.

The only thing that triggers that event in a single form is the RecordSelector so it is clearly meant for records only and aught right be called Record_Click. :rolleyes:

Without On ClickAnything each button has to have a one line procedure call the sub (or function).

My dream was in the style of:

Sub Form_ClickAnything()
MyAction
End Sub

Sub MyAction()
Dim ButtoName as String
ButtonName = Screen.ActiveControl.Name
' Do stuff with Buttonname
End Sub

Which is about 49 times lighter than having to include a Sub to call MyAction for every individual button.;)

One might extend it to checking for a naming pattern or Tag to only call the other sub if it was matched to the group to allow the presence of other buttons.

Doesn't seem like such a bad feature.
Maybe I should suggest it for Access 2013. :rolleyes:
I had a similar need. I found a solution that will fulfill your dream for both the mouse and the keyboard:
Code:
Private Sub Form_Load()
 Me.KeyPreview = True   ' Enable Form_KeyDown capturing keyevents before Controls_KeyDown
 ' Set _DblClick function for all Controls to one function:
  Dim ctr As Control
  For Each ctr In Me.Controls
    ctr.OnDblClick = "=DblClick()"      'Debug.Print ctr.Name
  Next
End Sub

Private Function DblClick()
  Debug.Print Me.ActiveControl.Name
End Function

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Debug.Print Me.ActiveControl.Name
End Sub
 

Users who are viewing this thread

Back
Top Bottom