?
Solved

Access 2010 adding a right Click short cut menu

Posted on 2011-09-19
12
Medium Priority
?
894 Views
Last Modified: 2012-05-12
Hi

How do I create and add a right click short cut menu to a control in Access 2010?

many thanks
0
Comment
Question by:kenabbott
  • 6
  • 4
  • 2
12 Comments
 
LVL 49

Accepted Solution

by:
Dale Fye earned 2000 total points
ID: 36560991
if you already have the shortcut menu, then you just need to assign it in the controls ShortcutMenu property in the properties dialog.

I you are talking about a "custom menu", I build mine in code.  Attached is a sample of the menu I display in my textboxes.  Paste the attached into a new code module, or into the forms code module if you don't want to make it callable from all forms.  There are a couple of other options in my default version, but those involve other forms.

When your application loads (Splash or Startup form), call the subroutine MenuText.

Then in design view of your form, select the control and set the ShortcutMenu property to "MyTextMenu"

Public Sub MenuText(Optional Reset As Boolean = False)

    Dim cbr As Object
    Dim cbrButton As Object

    'If the commandbar exists, and Reset is false, then exit
    If CmdBarExists("MyTextMenu") Then
        If Reset = False Then
            Exit Sub
        Else
            DeleteCmdBar "MyTextMenu"
        End If
    End If
        
    On Error GoTo TextMenuError

    DoCmd.Hourglass True

    Set cbr = CommandBars.Add("MyTextMenu", BarPopup, , True)

    With cbr

        Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
        With cbrButton
            .Caption = "&Copy"
            .Tag = "Copy"
            .OnAction = "=fnTextCopy()"
        End With

        Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
        With cbrButton
            .Caption = "Cu&t"
            .Tag = "Cut"
            .OnAction = "=fnTextCut()"
        End With

        Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
        With cbrButton
            .Caption = "&Paste"
            .Tag = "Paste"
            .OnAction = "=fnTextPaste()"
        End With

        Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
        With cbrButton
            .begingroup = True
            .Caption = "&Spell check"
            .Tag = "Spell check"
            .OnAction = "=fnTextSpell()"
        End With

        Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
        With cbrButton
            .begingroup = True
            .Caption = "&Zoom"
            .Tag = "Zoom"
            .OnAction = "=fnTextZoom()"
        End With
        
    End With

    DoCmd.Hourglass False
    Exit Sub
    
TextMenuError:
    MsgBox Err.Number & vbCrLf & Err.Description, vbInformation + vbOKOnly, "TextMenu error:"
    
End Sub

Public Function fnTextCopy()

    Dim frm As Form
    Dim ctrl As Control
    
    Set frm = Screen.ActiveForm
    Do While frm.ActiveControl.ControlType = acSubform
        Set frm = frm.ActiveControl.Form
    Loop
    Set ctrl = frm.ActiveControl
    
    If ctrl.SelLength = 0 Then
        ctrl.SelStart = 0
        ctrl.SelLength = Len(ctrl.Text)
    End If
    
    DoCmd.RunCommand acCmdCopy
        
End Function

Public Function fnTextCut()

    Dim frm As Form
    Dim ctrl As Control
    
    Set frm = Screen.ActiveForm
    Do While frm.ActiveControl.ControlType = acSubform
        Set frm = frm.ActiveControl.Form
    Loop
    Set ctrl = frm.ActiveControl
    
    If ctrl.SelLength = 0 Then
        ctrl.SelStart = 0
        ctrl.SelLength = Len(ctrl.Text)
    End If
    
    DoCmd.RunCommand acCmdCut
        
End Function

Public Function fnTextPaste()

    Dim frm As Form
    Dim ctrl As Control
    
    On Error GoTo TextPasteError
    
    Set frm = Screen.ActiveForm
    Do While frm.ActiveControl.ControlType = acSubform
        Set frm = frm.ActiveControl.Form
    Loop
    Set ctrl = frm.ActiveControl
    
    DoCmd.RunCommand acCmdPaste
    Exit Function
TextPasteError:
    If Err.Number = 2046 Then
        Resume Next
    Else
        DisplayError "Error encountered while attempting to paste text!"
    End If
End Function
Public Function fnTextSpell()

    Dim frm As Form
    Dim ctrl As TextBox
    
    On Error GoTo SpellError
    
    Set frm = Screen.ActiveForm
    Do While frm.ActiveControl.ControlType = acSubform
        If Application.Version > 11 And Application.Build < 6322 Then
            MsgBox "Unable to spell check this item!"
            Exit Function
        Else
            Set frm = frm.ActiveControl.Form
        End If
    Loop
    
    Set ctrl = frm.ActiveControl
    With ctrl
        
        If ctrl.SelLength = 0 Then
            ctrl.SelStart = 0
            ctrl.SelLength = Len(ctrl.Text)
        End If
    
    End With
    
    If ctrl.SelLength > 0 Then DoCmd.RunCommand acCmdSpelling
    Exit Function

SpellError:
    DisplayError "Error encountered by spell checker"
    
End Function

Public Function fnTextZoom()
    
    Dim Ctrl As Control
    Dim frm As Form
    Dim rs As DAO.Recordset
    Dim strCriteria As String

    Set frm = Screen.ActiveForm
    While frm.ActiveControl.ControlType = acSubform
        Set frm = frm.ActiveControl.Form
    Wend
    Set rs = frm.RecordsetClone
    Set Ctrl = Screen.ActiveControl

    DoCmd.RunCommand acCmdZoomBox
    
End Function

Open in new window

0
 
LVL 75
ID: 36561167
"How do I create and add a right click short cut menu to a control in Access 2010?"
Other than doing this in code, you can't.  This is highly unfortunate that the Custom menu tool was removed from A2010 (and A2007).

mx
0
 

Author Comment

by:kenabbott
ID: 36561205
"This is highly unfortunate that the Custom menu tool was removed from A2010 (and A2007)."

That was what was confusing me because I was sure I had used it in the distant past.  What was the point of removing it???
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 75
ID: 36561224
" What was the point of removing it???"
Absolutely none in my book.  Since previous menus, toolbars and right click shortcuts (from <=A2003) ARE still supported in A2010, the is no good reason that tool could not still be present ... in addition to the Ribbon paradigm.

Sad ...

mx
0
 

Author Comment

by:kenabbott
ID: 36561468
" Since previous menus, toolbars and right click shortcuts (from <=A2003) ARE still supported in A2010"

So presumably this means that I could run an A2003 db in 2010 and any custom right click menu would work but there is no way of importing them into a A2010 db??
0
 
LVL 75
ID: 36561520
"So presumably this means that I could run an A2003 db in 2010 and any custom right click menu would work"
Definitely.

"but there is no way of importing them into a A2010 db??"
Not that I've seen.

Also ... fyi ... you should check out this site at some point ... everything Ribbon:

http://www.accessribbon.de/en

I contacted him re adding on a tool to create Right Click menus ... but he said no time at the present.

mx

0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 36561529
I have not tried, and don't have 2010 available at the moment, and cannot remember the names for yet another user interface.  It used to be that you could import these custom menus using the Get External Data option.  In 2007, if you click the "Options" button on the Import Objects dialog, there is a checkbox for "Menus and Toolbars".  That is the first place I would start.
0
 
LVL 75
ID: 36561608
Dale ... you're RIGHT!  Man, I missed that one. That works in A2010 also. Once you select an MDB and the dialog opens to select objects, you can just select Options>>Menus & Toolbars ... and it will import the 'bars' onto the Add-Ins Ribbon.

That is good news to some extent.  It provides a workaround to still create custom bars in A2003 and them get them into A2010.  I need to test what happens if ... say I need to add an additional item to a right click menu ... then import into A2010 ... will get dups of everything already there ... plus the new added item ...?

mx
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 36561669

Joe,

You should also be able to use code similar to what I provided above to add controls to those "saved" commandbars, although I have never done this, and don't know whether those additions would also be saved.

I do know that the commandbars I create with the code I posted above must be recreated each time my application loads.
0
 
LVL 75
ID: 36561682
I imagine that works.  Just that ... it was soooooooooooooooooo EASY in <=A2003 to do this ... no code ... to create super cool 'bars', of which I have 100's.

mx
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 36561714
Joe,  

You could write an add-in, that we could all use.

I'm sure there has to be a way to make these command bars "permanent", I just have not figured out how to do it yet, so I continue to use the code method.  

I've got versions that use combo boxes and textboxes as well.
0
 
LVL 75
ID: 36561940
Yes, maybe I $$$could$$$ :-)

But they ... lets get the Ribbon guy to do this ... a tool within A2010 to create right clicks ... since he is already in this mode.

It might be possible to leverage something from this:

http://www.lebans.com/commandbars.htm

?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question