Hi everybody,
I'm using Access 2010, but the format for the database is .mdb because I'm not ready to fully convert it to Access 2010 and the .accdb format.
I've been reading up on how to create Shortcut Menus in Access 2010 using VBA and I'm pretty confident I understand how most of it works...meaning that I can create a custom shortcut menu, register it (for lack of a better term) so it displays in a form's/report's shortcut menu property dropdown, and have it display the commands.
However, I'm at a point now where I would like to create a custom sub/function and attach it to a command to be included on the shortcut menu for one or more forms/reports. I can create a custom sub/function and I know that I can attach it to a command button using the following: -
cmbControl.OnAction = "=MySubFunction()"
-
What I don't know how to do is to setup the sub/function so that it knows which form/report is calling the sub/function so that the name of the form/report can be used in the sub/funtion.
For instance, one of the things I'd like to do is modify the "Email as Attachment" command button so that it will always send as PDF instead of prompting the user for a list of choices (like .xps, .xlsx, etc.). If I was writing this as a private sub/function for a specific form/report, I would use Me.Name, but that won't work if I create the sub/function in a module (only public subs/function can be used in a module, right??).
In case it's needed, here's the code I'm using to create my custom shortcut menu, which I got from the Access Blog article written by Edwin Blancovitch: -
Option Compare Database
-
Option Explicit
-
-
Sub CreateReportShortcutMenu()
-
-
Dim cmbReportShortcutMenu As Office.CommandBar
-
Dim cmbControl As Office.CommandBarControl
-
-
'On Error Resume Next
-
-
'Delete the command bar if it already exists
-
DeleteContextMenu
-
-
' Create the shortcut menu.
-
Set cmbReportShortcutMenu = CommandBars.Add("ReportShortcutMenu", msoBarPopup)
-
-
With cmbReportShortcutMenu
-
-
' Add the Print command.
-
Set cmbControl = .Controls.Add(msoControlButton, 2521)
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Quick Print"
-
-
' Add the One Page command.
-
Set cmbControl = .Controls.Add(msoControlButton, 5)
-
-
' Add the Two Pages command.
-
Set cmbControl = .Controls.Add(msoControlButton, 639)
-
-
' Add the Print command.
-
Set cmbControl = .Controls.Add(msoControlButton, 15948)
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Select Pages"
-
-
' Add the Page Setup... command.
-
Set cmbControl = .Controls.Add(msoControlButton, 247)
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Page Setup"
-
-
' Add the Mail Recipient (as Attachment)... command.
-
Set cmbControl = .Controls.Add(msoControlButton, 2188)
-
' Start a new group.
-
cmbControl.BeginGroup = True
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Email Report as an Attachment"
-
-
' Add the PDF or XPS command.
-
Set cmbControl = .Controls.Add(msoControlButton, 12499)
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Save as PDF/XPS"
-
-
' Add the Close command.
-
Set cmbControl = .Controls.Add(msoControlButton, 923)
-
' Start a new group.
-
cmbControl.BeginGroup = True
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Close Report"
-
-
'*******************************************************
-
'* If I was going to use a custom sub/function,
-
'* this an example of how I would at least start it
-
'*******************************************************
-
-
' Add a blank command
-
Set cmbControl = .Controls.Add(msoControlButton, 1)
-
cmbControl.Caption = "Email As PDF"
-
cmbControl.OnAction = "=MySubFunction()"
-
-
End With
-
-
Set cmbControl = Nothing
-
Set cmbReportShortcutMenu = Nothing
-
-
End Sub
-
-
Sub DeleteContextMenu()
-
-
On Error Resume Next
-
CommandBars("ReportShortcutMenu").Delete
-
-
End Sub
-
Thanks,
beacon
Hi
One trick I have used in a 'universal' sub is this - Sub ActiveObjectClose(control As IRibbonControl)
-
DoCmd.Close Application.CurrentObjectType, Application.CurrentObjectName, acSaveNo
-
DoCmd.Restore
-
End Sub
This snipit is in a ribbon callback sub (runnning on 2k7 but an mdb format file), but it also works in 2k3 (without the ribbon bit!).
Don't know if it will help you but it just might as the active object should be the object subject to the right click (or its parent!?).
Obviously I haven't checked this as I run it from a menu (or, unfortunately, a Ribbon button).
MTB
3 13950
Hi
One trick I have used in a 'universal' sub is this - Sub ActiveObjectClose(control As IRibbonControl)
-
DoCmd.Close Application.CurrentObjectType, Application.CurrentObjectName, acSaveNo
-
DoCmd.Restore
-
End Sub
This snipit is in a ribbon callback sub (runnning on 2k7 but an mdb format file), but it also works in 2k3 (without the ribbon bit!).
Don't know if it will help you but it just might as the active object should be the object subject to the right click (or its parent!?).
Obviously I haven't checked this as I run it from a menu (or, unfortunately, a Ribbon button).
MTB
NeoPa 32,556
Expert Mod 16PB Beacon:
What I don't know how to do is to setup the sub/function so that it knows which form/report is calling the sub/function so that the name of the form/report can be used in the sub/funtion.
You seem to be setting things up to be called from a command button on a menu. As such, determining which form or report they are called from doesn't make sense. They are called from the menu (and specifically the Command you've set up). Beacon:
(only public subs/function can be used in a module, right??).
Only Public procedures can be called from outside of the module itself, if that's what you mean. Look up 'Scope' for a fuller understanding of this.
NB. Although standard modules are probably more appropriate for such things generally, I have no clear idea what you're trying to achieve, so I will mention, in case it helps, that Public procedures in Form/Report modules can also be accessed generally, but from outside themselves must be referred to with the full module name as prefix. EG : - Call Form_frmTest.PubProcIn_frmTest()
Thanks for the replies. Mike's suggestion actually led me to a solution, which I will provide below for anyone that wants to create a custom shortcut (right-click) menu in Access 2010 and wants to use a custom function attached to a command/control button. I may have caused confustion by referring to the button as a command button previously...after looking at the original example, I think it's called a control button since they are referenced as msoControlButton when adding the control.
Here's the CreateReportShortcutMenu and DeleteContextMenu code that is used to test for an existing shortcut menu of a certain name, delete it if it exists, and create the shortcut menu. The CreateReportShortcutMenu function has to be run once from the Immediate Window to register the shortcut menu so it will appear in the Shortcut menu dropdown in a form/report: -
Option Compare Database
-
Option Explicit
-
-
Public Sub CreateReportShortcutMenu()
-
-
Dim cmbReportShortcutMenu As Office.CommandBar
-
Dim cmbControl As Office.CommandBarControl
-
-
'On Error Resume Next
-
-
'Delete the command bar if it already exists
-
DeleteContextMenu
-
-
' Create the shortcut menu.
-
Set cmbReportShortcutMenu = CommandBars.Add("ReportShortcutMenu", msoBarPopup)
-
-
With cmbReportShortcutMenu
-
-
' Add the Print command.
-
Set cmbControl = .Controls.Add(msoControlButton, 2521)
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Quick Print"
-
-
' Add the One Page command.
-
Set cmbControl = .Controls.Add(msoControlButton, 5)
-
-
' Add the Two Pages command.
-
Set cmbControl = .Controls.Add(msoControlButton, 639)
-
-
' Add the Print command.
-
Set cmbControl = .Controls.Add(msoControlButton, 15948)
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Select Pages"
-
-
' Add the Page Setup... command.
-
Set cmbControl = .Controls.Add(msoControlButton, 247)
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Page Setup"
-
-
' Add the Mail Recipient (as Attachment)... command.
-
Set cmbControl = .Controls.Add(msoControlButton, 2188)
-
' Start a new group.
-
cmbControl.BeginGroup = True
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Email Report as an Attachment"
-
-
' Add the PDF or XPS command.
-
Set cmbControl = .Controls.Add(msoControlButton, 12499)
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Save as PDF/XPS"
-
-
' Add the Close command.
-
Set cmbControl = .Controls.Add(msoControlButton, 923)
-
' Start a new group.
-
cmbControl.BeginGroup = True
-
' Change the caption displayed for the control.
-
cmbControl.Caption = "Close Report"
-
-
'*******************************************************
-
'* I changed the .OnAction property below from my
-
'* original post, so please note the change.
-
'*******************************************************
-
-
' Add a blank command
-
Set cmbControl = .Controls.Add(msoControlButton, 1)
-
cmbControl.Caption = "Email As PDF"
-
cmbControl.OnAction = "=SendEmailAsPDFAttachment()"
-
-
End With
-
-
Set cmbControl = Nothing
-
Set cmbReportShortcutMenu = Nothing
-
-
End Sub
-
-
Public Sub DeleteContextMenu()
-
-
On Error Resume Next
-
CommandBars("ReportShortcutMenu").Delete
-
-
End Sub
-
I created the custom function SendEmailAsPDFAttachment and used Mike's suggestion to use the Application object to identify the current object, which in my case, is the name of the form/report where the control button is being pressed on the shortcut menu. As I may use this shortcut menu on multiple forms/reports, I need it to not be specific to any one form/report: -
Public Function SendEmailAsPDFAttachment()
-
-
Dim strSubject As String, strBody As String
-
-
On Error GoTo HandleError
-
-
strSubject = "Test Subject"
-
strBody = "Test Body"
-
-
DoCmd.SendObject acSendReport, Application.CurrentObjectName, acFormatPDF, , , , strSubject, strBody, True
-
-
SendEmailAsPDFAttachment = True
-
-
HandleError:
-
If Err.Number = 2501 Then
-
'Do nothing
-
Else
-
MsgBox "Unexpected error: " & Err.Number & " - " & Err.Description
-
End If
-
-
End Function
-
Thanks,
beacon
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Lex |
last post by:
I am writing a C# app that has a Menu. Some of the menu items will
have short cuts that do not exist in the Shortcut enum. I would like
the custom shortcuts to appear on the menu but as far as I...
|
by: Terry Bell |
last post by:
I have some code that adds entries to the "Module Compiled" popup
menu. Works fine in A97. You can open a module, right click, and the
new entries are there for you to click. I converted it to...
|
by: Salad |
last post by:
A97. If you set a forms properties to ShortcutMenu = No, the entire
form is disabled from displaying a shortcut menu.
If you set the property to Yes, all controls have a shortcut menu.
I have...
|
by: JMCN |
last post by:
hi
i'm having a problem with the customize a shortcut menu. i read the ms
office assistance: customize a shortcut menu/delete a command or add
to a shortcut menu and followed the simple...
|
by: downwitch |
last post by:
Hi all,
I've read through the archives on this, and scoured the web, to little
avail.
There has to be a way to move custom menu bars (or menubars, or command
bars, or popup command bars, or...
|
by: Linds |
last post by:
I have a report within my access database that I would like to have a
shortcut on my desktop that could bring up that report.
In other words, now I have to double click on the database, then go...
|
by: Jim Devenish |
last post by:
I have successfully added shortcut menus to Forms but I am now having
difficulty when I attach a shortcut menu to a text control. I have a
shortcut menu entitled 'CalendarEntries' with one command...
|
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...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |