Results 1 to 2 of 2
  1. #1
    Benaal is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2014
    Posts
    6

    Multiple Buttons on form, mapping to one event handler?

    Hi all,



    I have seen a few potential solutions on this one, but none of them seem to make sense to me. I was hoping that as well as suggesting code, someone might be able to explain in laymans terms what was going on?

    I have buttons on my main form (btn_1_1, btn_2_1, btn_2_2, etc.) which represent different parts of a machine. When clicked, I would like to capture the name of the button clicked and pass the variable to a function stored in a module.

    The passing of the variable is easy. I can take care of that. However, I have well over 100 buttons and i'm not keen on creating a sub for each button to handle the onClick event. I need some sort of Global Button Click Event Handler that will listen for button clicks and provide the name of a clicked button.

    Suggestions so far include adding all button controls to a collection (though I lost it from there), and also a VB.net solution (as below) which doesn't play nicely in Access 2010.
    Code:
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each Control As Control In Me.Controls
                If Control.GetType = GetType(TextBox) Then
                    If Control.Name.ToString.StartsWith("Lbl") Then
                        Dim TextBox As TextBox = DirectCast(Control, TextBox)
                        AddHandler TextBox.TextChanged, AddressOf TextBoxTextChanged_Handler
                    End If
                End If
                If Control.GetType = GetType(ComboBox) Then
                    Dim ComboBox As ComboBox = DirectCast(Control, ComboBox)
                    AddHandler ComboBox.SelectedIndexChanged, AddressOf ComboboxSelectedIndexChanged_Handler
                End If
            Next
        End Sub
    Any help would be appreciated.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You could have a function that used ActiveControl to identify the button and call that from each button. Rather than having code behind each button to call the function, you can put this in the event (presumes the function is named HandleClick):

    =HandleClick()
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. External script to call Command Button Click event handler
    By Sean Nakasone in forum Programming
    Replies: 5
    Last Post: 10-29-2014, 12:35 PM
  2. Access Form Requery (F5 on keyboard or reopening) handler?
    By superfury in forum Code Repository
    Replies: 0
    Last Post: 08-23-2013, 03:17 AM
  3. OnClose event and exit/quit buttons
    By atran in forum Access
    Replies: 1
    Last Post: 07-24-2012, 04:14 PM
  4. Replies: 11
    Last Post: 07-27-2011, 05:51 PM
  5. form - subform field mapping
    By nqirar in forum Programming
    Replies: 2
    Last Post: 03-02-2009, 01:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums