Context Menu (1 Viewer)

ECEK

Registered User.
Local time
Today, 19:53
Joined
Dec 19, 2012
Messages
717
I found this code to produce a context menu for runtime users.

Ive just created a new module, pasted this code and called it from the form. However I've saved the module but it doesn't come up in the dropdown section for the Shortcut Menu Bar.

Any ideas what I'm doing wrong?

Code:
Option Compare Database
Option Explicit

Private Sub CreateContextMenu()

Const strMenuName As String = "Form1_CommandBar"

Dim cbar As CommandBar
Dim bt As CommandBarButton
    
    Set cbar = CommandBars.Add(strMenuName, msoBarPopup, , False)
    Set bt = cbar.Controls.Add
        bt.Caption = "Cut"
        bt.OnAction = "=fCut()"
        bt.FaceId = 21
    Set bt = cbar.Controls.Add
        bt.Caption = "Copy"
        bt.OnAction = "=fCopy()"
        bt.FaceId = 19
    Set bt = cbar.Controls.Add
        bt.Caption = "Paste"
        bt.OnAction = "=fPaste()"
        bt.FaceId = 22
End Sub

Function fCut()
On Error Resume Next
    Application.CommandBars.ExecuteMso ("Cut")
End Function

Function fCopy()
On Error Resume Next
    Application.CommandBars.ExecuteMso ("Copy")
End Function

Function fPaste()
On Error Resume Next
    Application.CommandBars.ExecuteMso ("Paste")
End Function

highlighted
cbar As CommandBar

I'm getting a Compile error: User-defined type not defined
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:53
Joined
May 7, 2009
Messages
19,245
add reference to Microsoft Office xx.x Object Library
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 19:53
Joined
Jul 9, 2003
Messages
16,282
Try:-
Code:
Public Sub CreateContextMenu()
.....
.......

Sent from my SM-G925F using Tapatalk
 

ECEK

Registered User.
Local time
Today, 19:53
Joined
Dec 19, 2012
Messages
717
Gizmo = Syntax error
Arnie = "I already have the object lirary reference.

It works it the original attached
I just can't get it to work in my form.
Could it be that there is something counter-productive with my form?
I can't get the module to appear in the dropdown.
It's as though it isn't saving.
 

Attachments

  • Right click context bar.accdb
    412 KB · Views: 546

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:53
Joined
May 7, 2009
Messages
19,245
it's working alright. you have to right-click on your mouse for the context menu to appear.
 

ECEK

Registered User.
Local time
Today, 19:53
Joined
Dec 19, 2012
Messages
717
The solution was quite simple.
I imported the module from the file that I attached as opposed to copying the code.
It works fine now.
Thanks for your contributions.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 19:53
Joined
Jul 9, 2003
Messages
16,282
The solution was quite simple.
I imported the module from the file that I attached as opposed to copying the code.
It works fine now.
Thanks for your contributions.


Well something doesn't make sense. If your Subroutine "Private Sub CreateContextMenu()" is in a stand-alone module, you cannot call it from a Form because the sub is prefixed with "Private". It can only be called from within the module. Therefore you must have other code in the module that calls subroutine.
 

Users who are viewing this thread

Top Bottom