Link to home
Start Free TrialLog in
Avatar of martmac
martmacFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Right click popup (cut,copy,paste) Access 2010 runtime

I am distributing an Access 2010 front end in runtime to users. I know that right click menus disappear at runtime, but understand that a command bar popup can be implemented. There are examples of this around and I think I understand the principles, but am struggling to implement and wonder if someone could offer some guidance?

If I can just get a steer, I am sure I can refine and implement it.

I have a number of forms that I want to implement this for and want to setup so that the popup appears only when right clicking text boxes and maybe drop down lists to that I can cop from to etc. The code I have to create the toolbar is below. First of all, I need to know if this is correct, as when I call the function I get  

invalid procedure call or argument. ( I do have a reference by the way to the Office 14 Object Library)

Public Function CreatePopup()
    
    Dim cmb As CommandBar
    Dim ctlCBarButton As CommandBarButton

      
    Set cmb = CommandBars.Add("GeneralClipboardMenu", msoBarPopup, False, False)
    Set ctlCBarButton = cmb.Controls.Add(Type:=msoControlButton)

        With cmb
            .Controls.Add msoControlButton, 21, , , True  ' Cut
            .Controls.Add msoControlButton, 19, , , True  ' Copy
            .Controls.Add msoControlButton, 22, , , True  ' Paste
        End With
        With ctlCBarButton
            .Caption = "Hint"
            .FaceId = 124
            .Visible = True
            .OnAction = "controlHelp"
        End With
        
        Set cmb = Nothing
        
End Function

Open in new window


It is my understanding that I need to call the above at startup to create the popup (but I might be wrong!)

Then there is the question of calling the popup when right clicking on a field


The associated code looks like this, but I can't seem to implement. (not sure where to put it) I realise that in this example it provides a right click option for combo box and also subform. I do need to implement it for a couple of sub forms, so I am interested in that. I am on a really tight schedule to implement this and would really appreciate some guidance.

Sub getRightClick(ByVal actFrm As Form)
If dbgMode Then On Error GoTo Err_H:
    
    Dim ctl As Control
    For Each ctl In actFrm.Controls
        If TypeOf ctl Is TextBox Then
            ctl.ShortcutMenuBar = "GeneralClipboardMenu"
        End If
        If TypeOf ctl Is ComboBox Then
            ctl.ShortcutMenuBar = "GeneralClipboardMenu"
        End If
        If TypeOf ctl Is CommandButton Then
            ctl.ShortcutMenuBar = "GeneralClipboardMenu"
        End If
        If TypeOf ctl Is SubReport Then
            ctl.ShortcutMenuBar = "GeneralClipboardMenu"
        End If
        
    Next
    
Exit_H:
   Exit Sub
Err_H:
       MsgBox "Please write this down:" & vbNewLine & vbNewLine & "1) " & Err.Number & Err.Description & vbNewLine & vbNewLine & "2) " & _
       "createMenu", vbCritical
       DoCmd.Close acForm, Screen.ActiveForm.Name
End Sub

Open in new window



Many thanks in advance. Once I understand it and can see it working, I am sure I can refine etc. Would just be good to get it working!!
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
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
I just realized you indicated "RunTime", which casts an entirely different perspective on things.  Although you can still create the popups, assigning them to the ShortCutMenuBar property of the controls will not work.  For some reason Microsoft decided to disable the shortcut menus in runtime a long time ago.

The only way I've been able to get shortcut menus to work in runtime is to use the MouseUp event (you could use mouse down but some events occur between the two and you may have to play with that a bit) of your various controls.

In that event, you can test for the button and call your popup, something like:

if Button = acRightButton then commandbars("GeneralClipboardMenu").showpopup

This becomes cumbersome if you have a lot of controls where you want to implement this functionality, but it is well worth it (in my humble opinion).
Avatar of martmac

ASKER

Thank you for your prompt and helpful response. I had read part 1 of your article which actually helped me focus my thoughts. It is really helpful to know that ".....When you create the commandbar object, the final argument of that line is 'Temporary'.  When set to False, that means that this commandbar will be a permanent part of your application, and you need not rebuild it each time the application opens. I have now adjusted that in my app.

What I ended up doing is using this code to build the menu

Public Function CreateCMenu()
'On Error Resume Next

    CommandBars("MyContext").Delete

    Dim cmb As CommandBar 'Object
    Dim cmbBtn1 As CommandBarButton 'Object
    Dim cmbBtn2 As CommandBarButton 'Object

    Set cmb = CommandBars.Add("MyContext", _
      msoBarPopup, False, False)    ' msoBarPopup = 5
    With cmb
      ' add cut, copy, and paste buttons with the
      ' "magic number" technique that assigns
      ' appearance and behavior. The magic number
      ' goes in as the second parameter
      .Controls.Add msoControlButton, _
        21, , , True  ' 21=Cut, msoControlButton=1
      .Controls.Add msoControlButton, _
        19, , , True  '19= Copy
      .Controls.Add msoControlButton, _
        22, , , True  ' 22=Paste
   
End Function

Open in new window


Then on the the Form Load I run this

    Dim ctl As Control
    Dim actFrm As Form
   
   
    For Each ctl In Me.Form.Controls
        If TypeOf ctl Is TextBox Then
            ctl.ShortcutMenuBar = "MyContext"
        End If
        If TypeOf ctl Is ComboBox Then
            ctl.ShortcutMenuBar = "MyContext"
        End If
    Next

This way I can control what controls trigger the menu. It works a treat and also at runtime.
Avatar of martmac

ASKER

Thanks again
when you say "also at runtime", do you mean you are deploying the application to people who do not have Access installed on their computers, so you are deploying the "runtime" version of Access?  Or do you simply mean that it works fine when you run the application?
Avatar of martmac

ASKER

That is correct Access 2010 runtime is installed on user desktops. I have just double checked by shadowing a machine and the right click menu is active and functioning.