Results 1 to 8 of 8
  1. #1
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Right click menu functions (command bars)

    I'm getting into adding right click menu "command bars" in a form.

    This is the environment as far as modules and procedures go:

    Menu program
    Form 1 > Form 1 Class module > Standard shared Form module
    Form 2 > Form 2 Class module > Standard shared Form module
    Form n > ...
    (The > means that one object calls another, Standard shared Form module for lots of routine form tasks.)

    The menu form builds a standard right click menu that works for 95% of the forms and their controls (textboxes, combo boxes)
    The app is multilingual, and the menu form knows the current language, so this is a good place to build the right click menu for all the forms to use, instead of building one unique right click menu each time a form loads.

    Looking at something as simple as displaying a help for a control, it makes sense to add one procedure in the Standard shared Form module. To do this help, I need the Me (the form) and an array that has some control variables. In my reading, the right click would call a public function. The problem I'm seeing is how to get the Me (form) and control array to the Standard shared Form module when a right click happens.

    I got it to work by adding a routine to each Form n Class module, but this is very repetitive and creates lots of command bars and functions, one for each form, which seems kind of silly.

    Is there a way to get at the Me of a form and an array tied to the form's VBA that a standardized function for right click menus can use?

    I looked at these two blogs, and I'm a bit lost (I'm not sure they address this problem):
    Add a Custom Right-Click Menu to an Access Control (nolongerset.com) (maybe, but it doesn't address the array part of the problem)


    Understanding and using CommandBars, Part II (Creating your own) | Experts Exchange (experts-exchange.com)

    Any tricks to do this?

  2. #2
    Edgar is online now Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    Quote Originally Posted by twgonder View Post
    I need the Me (the form) and an array that has some control variables. In my reading, the right click would call a public function. The problem I'm seeing is how to get the Me (form) and control array to the Standard shared Form module when a right click happens.

    I got it to work by adding a routine to each Form n Class module, but this is very repetitive and creates lots of command bars and functions, one for each form, which seems kind of silly.

    Is there a way to get at the Me of a form and an array tied to the form's VBA that a standardized function for right click menus can use?
    Since Me is only valid in the context of the Form and Me is a Form object, you can create your necessary form type variables in your shared module and then set them from your form class modules. You could also reference a form from the shared module using the Forms collection from anywhere in your application, just remember form objects don't exist if they're not open, so handle that.

  3. #3
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @Edgar, post #2
    I've got instances of the form, so I'm not sure how to find the right one if I reference it from the shared module.
    Then there is still the issue of the array that I need too.

    It seems odd to me that Access has a hook for right clicks, but no way of knowing what form or control called it in the resulting function without jumping through a bunch of object searching.

  4. #4
    Edgar is online now Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    Correct me if I'm wrong, but a context menu should only exist once, right? then why would it not work if from your shared module you do this:
    Code:
    Public ContextMenuCaller As Form
    And from your Form class, from the procedure that calls the context menu:
    Code:
    Set ContextMenuCaller = Me
    And, if you also need to pass other parameters, then, instead of the previous lines, from your shared module:
    Code:
    Public ContextMenuData As Collection
    And from your form class:
    Code:
    Set ContextMenuData = Nothing
    Set ContextMenuData = New Collection
    With ContextMenuData
        .Add Me
        .Add Param1
        .Add Param2
        ' etc
    End With
    So, from your shared module, you can access the form who called the context menu with ContextMenuData(0), and do something similar with the other parameters.
    Last edited by Edgar; 05-20-2023 at 02:58 PM. Reason: handled extra stuff

  5. #5
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @Edgar, post #4 I haven't tried anything like that before, it looks interesting. Is it possible, if two different forms are opened that the ContextMenuData could be corrupted for the first form when the second gets instanced?

  6. #6
    Edgar is online now Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    It should not, because there can only be one instance of the variable running, which means the last form is always going to be the one using it. Why would we want a context menu open in a form that has lost focus? It should always be destroyed if another form calls it. I guess.

  7. #7
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @ Edgar, post#6 A user can have two forms open at the same time, and toggle between them. That's Windows!
    We've seen before, in another post that forms smash on top of the variables of another form.
    https://www.accessforums.net/showthr...676#post509676

  8. #8
    Edgar is online now Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    Maybe check the hwnd property of each form?

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

Similar Threads

  1. Right-click menu
    By pdanes in forum Forms
    Replies: 8
    Last Post: 10-21-2019, 08:29 AM
  2. Right click menu with arrow
    By loy in forum Programming
    Replies: 9
    Last Post: 05-15-2017, 12:51 PM
  3. Simple right-click menu example
    By pkstormy in forum Code Repository
    Replies: 1
    Last Post: 02-03-2015, 06:49 PM
  4. right click context menu
    By rpeare in forum Programming
    Replies: 8
    Last Post: 10-27-2014, 11:16 AM
  5. Replies: 3
    Last Post: 04-19-2013, 07:09 AM

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