Link to home
Start Free TrialLog in
Avatar of Clive Beaton
Clive BeatonFlag for Australia

asked on

Problem with Shortcut Menu Bar

I have created a Shortcut Menu Bar and applied it to the text box control but Rightt Click on the control doesn't show it.

I create the control on the Load event of the form.

Any clues?

Thanks in advance.  User generated image
Sub CreateCopyAddressShortcutMenu()

    Dim cmbRightClick As Office.CommandBar
    Dim cmbControl As Office.CommandBarControl

   On Error Resume Next
   Application.CommandBars("Copy Address").Delete
   On Error GoTo 0
   
   ' Create the shortcut menu.
    Set cmbRightClick = CommandBars.Add("Copy Address", msoBarPopup, False, True)

    With cmbRightClick
        
        ' Add Copy.
        Set cmbControl = .Controls.Add(Type:=1)
        cmbControl.Caption = "Copy Address"
        cmbControl.OnAction = "=TestMsg()"
        cmbControl.FaceId = 0

    End With
    Set cmbControl = Nothing
    Set cmbRightClick = Nothing
    MsgBox "Done"

End Sub

Public Function TestMsg()

   MsgBox "Test Message"

End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You need the line in Bold below:

    Dim cmbRightClick As Office.CommandBar
    Dim cmbControl As Office.CommandBarControl

   On Error Resume Next
   Application.CommandBars("Copy Address").Delete
   On Error GoTo 0
   
   ' Create the shortcut menu.
    Set cmbRightClick = CommandBars.Add("Copy Address", msoBarPopup, False, True)

    With cmbRightClick
       
        ' Add Copy.
        Set cmbControl = .Controls.Add(Type:=1)
        cmbControl.Caption = "Copy Address"
        cmbControl.OnAction = "=TestMsg()"
        cmbControl.FaceId = 0
    End With
    Me.ShortcutMenuBar = Application.CommandBars("Copy Address").Name
    Set cmbControl = Nothing
    Set cmbRightClick = Nothing
    'MsgBox "Done"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Clive Beaton

ASKER

MX suggestion worked fine for a form but I couldn't get it to work in a sub form.  

fyed's suggestion worked for a sub form but the shortcut menu stayed displayed until I moved out of that field.

Any suggestions as to how to make it disappear once clicked on?
I guess I didn't see it was a subform. Not sure why that would not work

Also, re "but the shortcut menu stayed displayed until I moved out of that field."
Now, I didn't try that on a subform, but ... once displayed via the mousedown of the right click button ... as soon as I left click anywhere, it goes away.  Are you not getting that result ?  That's pretty much how the default popups work.

Also, again ... was there a particular reason why you are opting for a code solution as opposed to the 'easy way' shown in the link I posted ?

mx
When I left click on an item in the the shortcut menu it runs the associated function and redisplays itself, almost instantaneously but visibly.  

I didn't use your easy way because, to the best of my knowledge, it is not available in Access 2010.
Try moving the code from the MouseDown event to the MouseUp event.
"I didn't use your easy way because, to the best of my knowledge, it is not available in Access 2010. "
Well, that is certainly correct.  I didn't realize you were in A2010.

"When I left click on an item in the the shortcut menu it runs the associated function and redisplays itself, almost instantaneously but visibly.  "

That only happens for me if I left click *on* the menu.

mx
I found a workaround by SendKeys Tab and BackTab but I would prefer it to work properly.

I've shared the points as I appreciate your help.

crb