473,395 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I create a custom right-click menu?

Seth Schrock
2,965 Expert 2GB
In looking online I have come up with the following code.
Expand|Select|Wrap|Line Numbers
  1. Public Sub CreateMenu()
  2. On Error GoTo Error_Handler
  3.  
  4. Dim cmdBar As CommandBarControl
  5.  
  6. On Error Resume Next
  7. CommandBars("MyMenu").Delete
  8. On Error GoTo Error_Handler
  9.  
  10. With CommandBars.Add(Name:="MyMenu", Position:=msoBarPopup)
  11.     Set cmdBar = .Controls.Add(Type:=msoControlButton)
  12.     cmdBar.Caption = "Add Database"
  13.     cmdBar.OnAction = MenuAddDatabase
  14.  
  15.     Set cmdBar = .Controls.Add(Type:=msoControlButton)
  16.     cmdBar.Caption = "View Roles"
  17.     cmdBar.OnAction = MenuViewRoles
  18.  
  19.     Set cmdBar = .Controls.Add(Type:=msoControlButton)
  20.     cmdBar.Caption = "Delete Database"
  21.     cmdBar.OnAction = MenuDeleteDatabase
  22.  
  23. End With
  24.  
  25. Exit_Procedure:
  26.  
  27.     Exit Sub
  28.  
  29. Error_Handler:
  30.     Call ErrorMessage(Err.Number, Err.Description, "modUtilities: CreateMenu")
  31.     'TSCs_ReportUnexpectedError "CreateMenu", "modUtilities", "Custom info"
  32.     Resume Exit_Procedure
  33.     Resume
  34.  
  35. End Sub
Each of the functions in the cmdBar.OnAction commands just produce message boxes to let me know that the menu works. In my form's Mouse_Up event, I have the following code.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. On Error GoTo Error_Handler
  3.  
  4. If Button = acRightButton Then
  5.     CommandBars("MyMenu").ShowPopup
  6.     DoCmd.CancelEvent
  7. End If
  8.  
  9. Exit_Procedure:
  10.  
  11.     Exit Sub
  12.  
  13. Error_Handler:
  14.     Call ErrorMessage(Err.Number, Err.Description, "Form_sfrmEmployeeDatabases: Form_MouseUp")
  15.     'TSCs_ReportUnexpectedError "Form_MouseUp", "Form_sfrmEmployeeDatabases", "Custom info"
  16.     Resume Exit_Procedure
  17.     Resume
  18.  
  19.  
  20. End Sub
When I right click, the three message boxes appear one after the other before the menu even appears. When I click OK out of the last one the menu then appears. However, clicking the buttons doesn't produce the message boxes. I also can't have all the functions running before I make a selection as one of the function will turn into deleting the selected record.

I'm not sure what to do to get it to run my functions when I click the buttons and not before the menu appears.
May 23 '13 #1
4 19317
ADezii
8,834 Expert 8TB
The following shortened Code will work as intended. Do you forget to place Parentheses around the Function Names in CreateMenu()?
Expand|Select|Wrap|Line Numbers
  1. Public Sub CreateMenu()
  2. On Error GoTo Err_Procedure
  3. On Error Resume Next
  4. Dim cmbCtl As CommandBarControl
  5.  
  6. On Error GoTo 0
  7. CommandBars("MyMenu").Delete
  8.  
  9. With CommandBars.Add(Name:="MyMenu", Position:=msoBarPopup)
  10.  
  11. Set cmbCtl = .Controls.Add(Type:=msoControlButton)
  12.     cmbCtl.Caption = "View Rules"
  13.     cmbCtl.OnAction = "MenuViewRoles"
  14. End With
  15.  
  16. Exit_Procedure:
  17.   Exit Sub
  18.  
  19. Err_Procedure:
  20.   MsgBox Err.Description, vbExclamation, "Error in CreateMenu()"
  21.     Resume Exit_Procedure
  22. End Sub
May 23 '13 #2
Seth Schrock
2,965 Expert 2GB
Did you mean double quotes instead of parenthesis around the function names? I did try having double quotes around the function names, but they still didn't run in my code. When I get to work tomorrow, I'll give yours a try.

Just curious... On one website, I read that the menu only needed created once. Do I just run CreateMenu once (like maybe in the database opens) or do I need to call it every time I right click?
May 24 '13 #3
Seth Schrock
2,965 Expert 2GB
I solved it. I had the called functions set as private functions. Switching them to public functions fixed it. I also enclosed the function names in double quotes.

I remember reading that it had to be public functions, but I guess it went in one ear and out the other. Or would that be in one eye and out the other. Oh well, at least I solved it. thanks for your help ADezii.
May 24 '13 #4
ADezii
8,834 Expert 8TB
Anytime, Seth. The pleasure was all mine.
May 24 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: balg | last post by:
Is it possible to have a custom main menu which can be positioned other than dock.top, mainly below the tool bar ? is there any such freeware main menu component available...
2
by: dennishancy | last post by:
On my web site, I have a "Links" link. Rather than create a separate HTML file listing all the links, I'd like to create a popup menu (not sure if that's the official name or not). So, when I...
2
by: Larry R Harrison Jr | last post by:
I have Access 97. I know how to create custom command bars. I typically create pull-down menus; seldom do I create toolbars. But I have a case where I would; in fact, I would like to have a menu...
2
by: Dennis C. Drumm | last post by:
This is a restatement of an earlier post that evidently wasn't clear. I am building a custom MessageBox dialog that has, among other things, a few new button options (yes to all, no to all, etc.)...
6
by: Dave | last post by:
Hello there. I'm interesting how do I create server side menu in ASP.NET, I was searching for some menus, but they are all done with DHTML or JavaScript, so I have some problems to use them. I...
8
by: Ken Yu | last post by:
Hi, How can i disable "RightClick Menu" in Internet Explorer, when access the frontpage ? tks a lot ! Ken
5
by: rishabhshrivastava | last post by:
Hi All, I am using a Web Application and I want to customize Right Click Menu as following:- Do Nothing when user does a Right Click. When user select some text and does a Right Click he/she...
1
ganesanji
by: ganesanji | last post by:
hi I want to create a horizantal menu using css with html codings. Do you know please help me to write the coding. My question is that when we move the mouse over the menu then the menu contens...
2
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hello, I have an existing (WPF) Class Library project that I'd like to add a WPF Custom Control to. However, when I try to Add New Item, that isn't one of the available options. Perhaps this...
3
by: munkee | last post by:
Hi all, I have been playing with pivotcharts lately to display my data from my database. I have since found however that I will need to get rid of a lot of the "options" a user has to mess around...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.