• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 900
  • Last Modified:

Access 2010 adding a right Click short cut menu

Hi

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

many thanks
0
kenabbott
Asked:
kenabbott
  • 6
  • 4
  • 2
1 Solution
 
Dale FyeCommented:
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
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
"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
 
kenabbottAuthor Commented:
"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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
" 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
 
kenabbottAuthor Commented:
" 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
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
"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
 
Dale FyeCommented:
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
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
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
 
Dale FyeCommented:

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
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
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
 
Dale FyeCommented:
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
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now