Results 1 to 11 of 11

Thread: Multiple controls with same event handler ... how?

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    3

    Multiple controls with same event handler ... how?

    At the moment I have something like this (subroutines called via the onClick event in an Access 2003 DB):


    VB Code:
    1. Private Sub cmdSortAsc_Click()
    2.  On Error GoTo cmdSortAsc_Click_Err
    3.  
    4.       DoCmd.Echo False, "Sorting records ..."
    5.    
    6.          Screen.ActiveForm.AllowAdditions = False
    7.              DoCmd.GoToControl "txtCustomerID"
    8.              DoCmd.RunCommand acCmdSortAscending
    9.  
    10.                  Me![cmdSortDesc].Visible = True
    11.                  Me![cmdSortDesc].SetFocus
    12.                  Me![cmdSortAsc].Visible = False
    13.                 DoCmd.Echo True, ""
    14.    
    15.     Exit Sub
    16.  
    17. cmdSortAsc_Click_Exit:
    18.              DoCmd.GoToRecord , , acFirst
    19.    
    20.      Exit Sub
    21.  
    22. cmdSortAsc_Click_Err:
    23.      Resume cmdSortAsc_Click_Exit
    24.  
    25. End Sub
    26.  
    27. Private Sub cmdSortDesc_Click()
    28.  On Error GoTo cmdSortDesc_Click_Err
    29.      
    30.        DoCmd.Echo False, "Sorting records ..."
    31.  
    32.      Screen.ActiveForm.AllowAdditions = False
    33.          DoCmd.GoToControl "txtCustomerID"
    34.          DoCmd.RunCommand acCmdSortDescending
    35.    
    36.              Me![cmdSortAsc].Visible = True
    37.              Me![cmdSortAsc].SetFocus
    38.              Me![cmdSortDesc].Visible = False
    39.              DoCmd.Echo True, ""
    40.     Exit Sub
    41.  
    42. cmdSortDesc_Click_Exit:
    43.             DoCmd.GoToRecord , , acFirst
    44.  
    45.      Exit Sub
    46.  
    47. cmdSortDesc_Click_Err:
    48.      Resume cmdSortDesc_Click_Exit
    49.  
    50.  
    51. End Sub
    52. Private Sub cmdSortAscComp_Click()
    53. On Error GoTo cmdSortAscComp_Click_Err
    54.      
    55.      DoCmd.Echo False, "Sorting records ..."
    56.    
    57.         Screen.ActiveForm.AllowAdditions = False
    58.             DoCmd.GoToControl "txtCompanyName"
    59.             DoCmd.RunCommand acCmdSortAscending
    60.                
    61.                 Me![cmdSortDescComp].Visible = True
    62.                 Me![cmdSortDescComp].SetFocus
    63.                 Me![cmdSortAscComp].Visible = False
    64.                 DoCmd.Echo True, ""
    65.             Exit Sub
    66.  
    67. cmdSortAscComp_Click_Exit:
    68.             DoCmd.GoToRecord , , acFirst
    69.    
    70.     Exit Sub
    71.  
    72. cmdSortAscComp_Click_Err:
    73.     Resume cmdSortAscComp_Click_Exit
    74.  
    75. End Sub

    I don't like this, because it's very similar code for each cmdButton placed on the form and my gut tells me that I could have one routine that handles all of the desired buttons.

    This being the case, I need to be able to determine which object caused the event and I'm kinda going along these lines, but I have no idea if this is correct or not:

    VB Code:
    1. Private Sub cmdSort(ByVal sender As AccessObject)
    2.     On Error GoTo cmdSort_Err
    3.    
    4. ' do stuff
    5.    
    6. cmdSort_Exit:
    7.     DoCmd.GoToRecord , , acFirst
    8.    
    9.     Exit Sub
    10.    
    11. cmdSort_Err:
    12.      Resume cmdSort_Exit
    13. End Sub

    Each button does essentially the same thing, just sorting different columns through this type of code (which then varies on a per button/routine basis):

    VB Code:
    1. DoCmd.GoToControl "txtCompanyName"
    2.             DoCmd.RunCommand acCmdSortAscending
    3.                
    4.                 Me![cmdSortDescComp].Visible = True
    5.                 Me![cmdSortDescComp].SetFocus
    6.                 Me![cmdSortAscComp].Visible = False

    Two buttons are utilized and toggled in visibility to sort in one direction or another, each calls different routines, and then this is repeated per column that can be sorted.

    I can't help but feel that this is cludgey ... surely one routine could ascertain which button was pressed, therefore which column to sort and the requirement for buttons for each sort direction could be removed simply with the use of global variables that remember what's currently sorted and in what direction.

    All of that probably makes little sense as I tend to use 1,000 words where 100 will do, but can anyone help me tidy this code as I feel that it can be?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Multiple controls with same event handler ... how?

    Welcome to the Forums.

    You can simplify the sorting by using only one button. Then if clicked you
    change the caption of the button to the opposite direction of what it was
    before. Then in the code you can have one routine to sort based upon the
    button caption. No variables needed.

    It looks like you will still need the procedure for cmdSortAscComp since it is
    sorting on a different key field - txtCompanyName.


    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    3

    Re: Multiple controls with same event handler ... how?

    With multiple buttons (read: columns) being present, each sorting individual columns, how can I:

    a) Call one routine from different buttons? (There are 14 columns in all and the buttons serve as column headers).

    b) Have the routine pick up on the calling cmdButton (Ie, Know the object that caused the event).

    I apologize for my ignorance, but my strengths are in C# and web programming, so while I may feel that this is possible, it is somewhat frustrating being foiled by my lack of simplistic knowledge.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Multiple controls with same event handler ... how?

    Ok, you can have all your comand buttons click events make another call
    to a general sort procedure, passing the command buttons name. Then in the
    general sort procedure you can have variables for sort field and asc or desc.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    3

    Re: Multiple controls with same event handler ... how?

    So you have to pass a name? There isn't a reference to the object passed as part of the event? (I think here particularly of those languages that allow you to determine event type, the object that caused it and so forth).

    If I have a general procedure to call from all buttons, is this then placed as a module?

    Again, apologies, but using the interface and placing - say - cmdSort() as the property of the OnClick event produces the error "No such module", so what is the syntax to put here so that it will call a general subroutine?

  6. #6
    Lively Member
    Join Date
    Sep 2004
    Posts
    74

    Re: Multiple controls with same event handler ... how?

    Just create an public function something like :

    Code:
    Public Function SomeFunc(button As String)
    
    MsgBox button
    
    End Function
    and enter something like this where you normally select youre macro/vb code for youre onclick event :

    =Somefunc(variable etc etc)
    Attached Images Attached Images  

  7. #7
    Lively Member
    Join Date
    Sep 2004
    Posts
    74

    Re: Multiple controls with same event handler ... how?

    Quote Originally Posted by Kolyana
    So you have to pass a name? There isn't a reference to the object passed as part of the event? (I think here particularly of those languages that allow you to determine event type, the object that caused it and so forth).

    If I have a general procedure to call from all buttons, is this then placed as a module?

    Again, apologies, but using the interface and placing - say - cmdSort() as the property of the OnClick event produces the error "No such module", so what is the syntax to put here so that it will call a general subroutine?
    You cannot call as SUB, it has to be an Function and it has to be an Public function (location can be an module or the class of the calling form)

    syntax for calling see my previous post

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Multiple controls with same event handler ... how?

    I just had to add a comment.

    's Calibra on the great explaination!
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Lively Member
    Join Date
    Sep 2004
    Posts
    74

    Re: Multiple controls with same event handler ... how?

    Thanx

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Multiple controls with same event handler ... how?

    Or use vb.Net, where you can add any control to an event handler that takes the same arguments, a/o use delegation.
    Tengo mas preguntas que contestas

  11. #11
    Addicted Member
    Join Date
    Oct 2000
    Location
    Vienna/Austria
    Posts
    132

    Re: Multiple controls with same event handler ... how?

    hi togheter,

    have another interesting approach to the problem.

    If you catch the event of the commandbuttons then you have only one Eventhandler for all or for which commandbutton you want.

    The Example below is for all Commandbutton in a Form. In the attached ACCESS DB you will find also an approach for commandbuttons which you want.

    Happy new Year

    TheOnly

    VB Code:
    1. '---- put this in a Form
    2.  
    3. 'define a collection
    4. Private col As New Collection
    5. 'initialise the Commandbutton event class
    6. Private NewCommandButton As New clsEvents
    7.  
    8.  
    9.  
    10. Private Sub Form_Close()
    11.     'delete all references
    12.     Set col = Nothing
    13.     Set NewCommandButton = Nothing
    14. End Sub
    15.  
    16. Private Sub Form_Load()
    17.  
    18. 'load all the commandbutton of the form to one Eventhandler
    19.  
    20.     Dim ctl As Control
    21.     'loop through all controls
    22.     For Each ctl In Me.Controls
    23.         'check if control is a commandbutton
    24.         If ctl.ControlType = acCommandButton Then
    25.             'set a new reference to the new Eventclass
    26.             Set NewCommandButton = New clsEvents
    27.             'store the commandbutton to the eventclass
    28.             Set NewCommandButton.CmdBtn = ctl
    29.             'store the eventclass in a collection to keep them alive
    30.             col.Add NewCommandButton, ctl.Name
    31.         End If
    32.     Next ctl
    33.  
    34. End Sub
    35.  
    36. '--- put this in clsEvent class module
    37.  
    38. 'catch the event of Commandbuttons
    39. Private WithEvents m_CommandButton As CommandButton
    40.  
    41. 'which commandbutton should have a common Eventhandling
    42. Public Property Set CmdBtn(ctl As CommandButton)
    43.     Set m_CommandButton = ctl
    44.     'set the commandbutton event
    45.     m_CommandButton.OnClick = "[Event Procedure]"
    46. End Property
    47.  
    48. Private Sub Class_Terminate()
    49.     Set m_CommandButton = Nothing
    50. End Sub
    51.  
    52.  
    53. Private Sub m_CommandButton_Click()
    54. 'place here the code
    55. MsgBox "You clicked: " & m_CommandButton.Name & " with Caption " & m_CommandButton.Caption
    56.  
    57. End Sub
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width