Right click context menu

ECEK

Registered User.
Local time
Today, 04:40
Joined
Dec 19, 2012
Messages
717
I have downloaded the attached database that perfectly illustrates how to create a right click menu.

What I want to do is get someone to explain how I can use this in MY database.

What I am struggling with is "Calling" the creating of the menu.

Thanks
 

Attachments

The "CreateContextMenu" routine was run once and the command bar Form1_CommandBar was permanently created in this database. To show this, open the database and comment out the CreateContextMenu routine. Then open the database again and confirm that the menu is still there. You can do...
Code:
? commandbars("Form1_CommandBar").Name
Form1_CommandBar
...in the immediate pane, which shows that Form1_CommandBar is a permanent member of the CommandBars collection.

Also, check the ShortcutMenuBar property of the Other tab of the form's property sheet in design view. It says Form1_CommandBar, so that is how that form knows to show it.

hth
 
Hi MarkK
I guessed that this was the case. i.e. that the "create" code had already been run.

with this in mind:
How would I easily call this code in a new database/

I have found a solution (I imported both the Form1 and Module1 into my new database in addition to selection the option of importing Menu's and Toolbars)

So whilst I have found my solution I'm none the wiser !!!

1.How do you execute the "create" code in the first place?
2.How do I know that it has worked?
3.Where can I view my created menu?
 
Well, you can run the code in a number of ways. Call it from other code, like a button click handler, or in a code window click the first line of the procedure and press F5.

To know that it has worked, check the CommandBars collection for the name of the item you created...
Code:
Function CommandBarExists(Name as string) as Boolean
on error resume next
   CommandBarExists = Typename(CommandBars(Name)) = "CommandBar"
End Function

To view the created popup menu, you can show it programmatically, like...
Code:
CommandBars("YourPopupName").ShowPopup

hth
 
Hi MarkK
I imported the Form1 and the Module1 into a new database (but not the Menus etc

I pressed F5 on the "Const strMenuName As String = "Form1_CommandBar""
and received:

Compile Error:
User-defined type not defined

This line of the code was highlighted.
Dim cbar As CommandBar

here is the full code again:
Code:
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

Function CommandBarExists(Name As String) As Boolean
On Error Resume Next
   CommandBarExists = TypeName(CommandBars(Name)) = "Form1_CommandBar"
End Function
 
You need to activate a reference to the Microsoft Office XX.0 Object Library. XX will be replaced with whichever version number applies to your version of Access.
 

Users who are viewing this thread

Back
Top Bottom