Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280

    Pass parameter to event handler when control on form clicked


    I have a form with numerous identical controls. I want to create a single event handler for the On Click Event for each, rather than creating one coded event handler for each. I have read that this can be done by calling a Function in a Module - as below.
    Attachment 52552

    The ActiveControl parameter is supposed to identify the Control that has just been clicked - in this case imgCoin1, but it is not doing so. It is giving the name of the previous Control that had the focus.
    Why is this not working? Is there another way of identifying the Actual Control that was clicked.
    Of course, I could just put a hard coded parameter in each click event to identify the control - i.e. 1, 2, 3, 4 etc, but I was hoping to avoid doing this.
    Attached Thumbnails Attached Thumbnails Untitled-1.jpg  

  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,652
    As far as I know that's only valid in VBA. I'd drop the parameter and get the active control within the function:

    https://learn.microsoft.com/en-us/of....ActiveControl
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2021
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    I agree with Paul. You don't need to pass ActiveControl as an argument. The whole point of ActiveControl is to avoid having to do so.
    I also wonder how Access handles coincidental On_Click and Mouse_Down. Who's on first?

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    It is giving the name of the previous Control
    Are you certain that's not coming from the mousedown event? That event is going to fire first if you click on the control rather than tab into it. If the linked example isn't to your liking, maybe what you want is a class module that will detect any click event and act on textboxes only. Pretty sure that has be covered in here already. The only way I've done this was to write one function that accepts the name of the control. However, that requires one to write the control name within the parentheses for each control that you want to pass.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Miles R is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Thanks for the replies. Firstly, I am using VBA in Access from 64 Bit Office 365 on a new Windows 11 machine. I have updated my profile to reflect this now.
    I got the idea to put ActiveControl as a parameter from someone who had the same problem and they seemed to imply this worked. No doubt as replies have mentioned, they could have removed the parameter and just put it in the event handler.
    Yes, the MouseDown Event does fire first, but this also suffers from the same problem. I put a Msgbox ActiveControl.Name statement as the very first line of the event handler and it is showing the previous control that had focus. I suspect the MouseDown event may not set the ActiveControl anyway. Not sure about that.
    I also suspect it may be a timing issue and if I waited a while using the timer function, it may well update the active control to the one clicked.
    However, using a timing event is an unnecessary complication and I will probably resort to hard coding the control number (1,2,3 etc) in each controls click and mouse down events. At least I can dispense with 100 or so coded event handlers in the forms code.
    p.s. When researching this, someone said that the Function had to be a Public Function in a Module rather than the Form code. I have not found this to be the case, as I have put the EventOpenIndividual function as a Private Function directly in the Form code. It works.
    I am still hoping that there is a parameter that I can pass that identifies the Control Name itself e.g. "=EventOpenIndividual(ControlName)", but not found anything that works as yet. Bit surprising that there is not a way to do this.
    Thanks again for the replies.

  6. #6
    Miles R is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Just come across another problem. I want to do the same for the MouseDown Event, but need to know whether it was left or right mouse click.
    In a normal event handler, the event handler would pass parameters such as
    Private Sub imgCoin1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = acRightButton then it is Right Click.

    But how is this done in a Function Call? "=MouseDownEvent(Button)" does not work.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    ChrisO, a former participant in many forums (now deceased), provided info on class modules and event handlers.
    See this post and others by ChrisO in that thread for more info.

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    This link states the order of events

    https://support.microsoft.com/en-us/...7-ce86553682f9

  9. #9
    Miles R is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Thanks for the reply.
    I've had a look at this and although it does not provide an immediate answer, I will have a look further in that direction. After all, the MouseDown event itself does know whether it was left or right click, so it must be doing something behind the scenes to determine it. Its probably quite simple.

  10. #10
    Miles R is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Thanks, useful but does not solve my issue.

  11. #11
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    Not sure who you are replying to, me or orange.

    if you are assigning functions to events using =somefunction() in the property sheet, that is fine for those events that do not have parameters (such as load/current). If you need to do this for an event that has parameters (such as mousedown) and you want to reference those parameters you have to use a class. There is no other way other than using the normal event procedure and calling whatever function you are using from there.


    see this link, you might find it more useful
    https://www.msaccesstips.com/2019/04...ss-module.html

  12. #12
    Miles R is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Was replying to CJ on the useful order of events list, although I had investigated order of event before, so I knew Mouse Down came before Click.

    I have actually solved one of the problems - that of determining whether Left or Right Mouse click.
    In my MouseDown event handler, I call a function "=MouseDownEvent(1)", where the parameter is 1, 2, 3,... for the 55 controls that have this event.

    The MouseDownEvent is as follows :-

    Code:
    Private Function MouseDownEvent(pintImage As Integer)
        If GetAsyncKeyState(VK_RBUTTON) Then
            code for right button pressed
        Else
            code for left (or any other button pressed)
        End If
    End Function
    I have declared a standard DLL function to determine the key press (works for any key and the mouse buttons)
    Code:
    Public Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Public Const VK_RBUTTON = &H2 'Right mouse button
    This seems to work and I have now removed all explicit event handlers for the mouse down and click events for each of my 55 controls.
    Just one Function Event handler needed.

    Just one more problem to solve, which is how to pass the control name to the MouseDownEvent function without having to give a different parameter for each. I have another Form with 150 controls.

  13. #13
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    You can pass parameters in a property sheet function call, such as =myCtl(Tag) where the public module level function myCtl raises a message box with the .Tag value.
    I wouldn't try to pass the clicked button value when you can just test for it in the procedure. Do one thing if it was left button, something else if the right button.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    Miles R is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Thanks for this. I will have to research this further, as I don't understand the .Tag value thing you mentioned. As long as the .Tag is generic and does not have to be tailored for each control, otherwise I might just as well pass the 1,2,3.. parameter.
    I assume you saw my previous post about solving the Left/Right mouse button problem.

  15. #15
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    .Tag was just an example of a parameter that I thought might be useful since you could edit a tag and maybe not have to edit code. You could pass a simple string or number instead. Yes, you would have to edit every function call for every control you need this for, but at least you'd only have one function to write. As I mentioned, pretty sure there are posts in here somewhere that contain db attachments that use a class for this.

    Yes I saw about the left/right buttons, which is why I said I wouldn't bother to try and pass the button - just code for the button. If you need the button value, why not just get it from the mouse event since it's already 'created' that value for you?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 03-11-2015, 12:48 PM
  2. Replies: 1
    Last Post: 12-17-2014, 11:53 AM
  3. 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
  4. Replies: 4
    Last Post: 02-14-2013, 10:06 AM
  5. Replies: 3
    Last Post: 05-07-2012, 12:17 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