Where to implement callback in ribbon xml with Access - ms-access

I created a ribbon xml in Access 2010, one of the related element as follows:
<button id="button1" size="normal" label="Sample1" getImage="GetImage" onAction="OnMenuAction" />
I implemented OnAction in Main module as:
Public Sub GetImage(ByVal control As Office.IRibbonControl, ByRef image)
image = "HappyFace"
End Sub
Public Sub OnMenuAction(ByVal control As Office.IRibbonControl)
MsgBox "You've clicked the button " & control.ID & " on the Ribbon"
End Sub
The Tab did appear in ribbon, but without "HappyFace" image and shows error message:
"Access cannot run the macro or callback function 'GetImage'.
Make sure the macro or function exists and takes the correct parameters."
When I click the button, Access also can't find the callbacks and shows the Message:
"Access cannot run the macro or callback function 'OnMenuAction'.
Make sure the macro or function exists and takes the correct parameters."
My question is where should I put these callbacks besides Main module? Thank You.

Finally I solved the problem by including Microsoft Office 14.0 Object Library.

Related

Access Ribbon callback function cannot be inside report

Below is my ribbon code for report
<button id="EmailPDF" label = "Email Customer"
size="large"
imageMso="FileEmailAsPdfEmailAttachment"
onAction="=Rpt_Email()"
supertip= "Email to customer."/>
And this is my callback function
Public Function Rpt_Email() As Boolean
MsgBox "OK"
End Function
Call back function works, if it is in module. But, it does not work, if I move it to Report. I need it to be on the report. Because, I need to access each report information and setup email accordingly.
You can't do that. Callbacks need to be in public modules, and can't be in class modules, form modules or report modules.
Instead, use Screen.ActiveReport to determine which report is active from that global module. Or, create separate functions and ribbons per report, and show them as the report ribbon (this has the additional advantage of not showing the ribbon if none of the reports are open).

Access 2007 crashes when canceling On No Data and calling function from On Close

MS Access 2007 (and perhaps later versions) appears to have a bug that causes a hard crash when a report open is canceled in the On No Data event and the On Close event contains a call to a public function. This does not appear to be an issue with MS Access 2003 or earlier.
Steps to reproduce:
Create a new report in MS Access 2007 (this is not an issue in MS Access 2003 and prior)
Set the recordsource (it can be anything, but it must be a bound report)
Set event On No Data to [Event Procedure]
In the code module for the report, enter:
Private Sub Report_NoData(Cancel As Integer)`
Cancel = True
End Sub
Set event On Close to =Foo()
In a standard code module, add the following code:
Public Function Foo()
End Function
Open the report using a filter that excludes all data (in order to raise the No Data event)
Results:
Access immediately suffers a hard crash with the "Access has stopped working..." error message
Two questions:
Is there a workaround?
Is this a known issue? (known to Microsoft or the greater MS Access community)
Workaround
There are a couple of workarounds:
Replace the function call in the report's On Close property with "[Event Procedure]" and then call the function from within the report's Private Sub Report_Close() in VBA.
- OR -
Move the function call from the report's On Close property to its On Unload property. (Note: the On Unload report property was introduced with MS Access 2007)

Handling ActiveX events on an Access form

I have an ActiveX control for opening and manipulating a proprietary image type. I am able to embed the control in an Access form, and by using the Object Browser am able to ascertain all the class members. The only problem I am having is handling some of the events raised by the control.
For example, from the Object Browser I can see the following event definition:
Event RightClicked()
And when I add the following code to the form:
Private Sub CtrlInstanceName_RightClicked()
'Anything here
End Sub
Everything works as expected. However, if the event declaration has parameters passed to it, such as:
Event MeasurementUpdated(id as Long)
Adding:
Private Sub CtrlInstanceName_MeasurementUpdated(id as Long)
'Anything here
End Sub
Produces the following error:
The expression ... you entered as the event property setting produced
the following error: Procedure declaration does not match description
of event or procedure having the same name.
Even more strange after adding this second handler, all event handlers on the form produce this error, including events raised by the form and other controls on the form.
I have tested the ActiveX control in a C# .NET application and am able to handle the events there (although they don't quite work as expected). In Visual Studio, the function prototypes end up being a little odd with auto-defined event handler class types.
Any ideas on how I need to change the event procedure declaration to avoid this error?
Well like most Friday questions, I later found the answer to this one was rather quite simple.
First, due to my unfamiliarity with the VBA code editor, I did not know that it was possible to create stubs for embedded controls by using the Object combobox above the code. After selecting my control name I selected the MeasurementUpdate event from the procedure combobox.
The correct declaration is simply:
Private Sub CtrlInstanceName_MeasurementUpdated(ByVal id as Long)
'Anything here
End Sub
The key difference being the ByVal modifier.

ms-access vba Eval function behavior

I have a public function in an Access form
Public Function PopupProcess() as long
MsgBox Me.ActiveControl
PopupProcess = 1
End Function
When I call
eval("forms('MyForm').popupprocess")
it shows a message box 2 times. Does anybody know why it does that?
I Have Access 2003 with SP3.
EDIT : Main idea is to call function from that form for Custom Commandbar control OnAction.
Maybe you have better way to call function from a form for commandbar control.
This is a very long standing bug that’s been around since the days of access 97, (about 4-5 versions of access).
The solution here is to NEVER use the forms qualifier, simply place the following in your on action event, and you’ll be just fine
=PopUpProcess()
Note that you must precede it with=, and the suffix must have the brackets ()
Keep in mind that you can actually use behavior to your advantage. The function that runs is going to be from the form that currently has the focus on the screen. That means you can have different forms with the same name of the function, and whichever form has the focus, that function with that name will run from that forms code module.
Even better, if one of the forms does not have that function as public in the forms code module, then the function in a standard code module is used. So you might have nine forms, that all use the standard one function in the main standard code module. However, the 10th form might need to run special code, so you simply place that function code in the form’s code module as public and it will run in place of the public on in the standard code module.
This approach allows you to build a single custom menu bar that applies to many different forms, but those many forms will run different code on from that custom menu bar. This also encourages you to place the menu code in the form it belongs.
So to solve your problem, simply don’t use a form’s qualifier, and use the above format.
Note that you can pass Parameters from those functions also, for example
=PopUpProcess(‘hello’)
And then declare the function as:
Public Function PopUpProcess(strParm as string)
Keep in mind that the function and syntax and all of what I stated above also applies to when you use the on action in a ribbon for access 2007.
No idea. What happens if you call it like this?
Call Forms("MyForm").PopupProcess
Try using the CallByName function, instead of eval, to call your function. It should only fire your function once, and it will still allow you to parameterize the form name and the function or sub name:
CallByName Forms("MyForm"), "PopupProcess", VbMethod

How to execute code from a Module that resides in Microsoft Office Access Class Object

Is it possible to execute code that is apart of a Form in MS Access from an outside module? What would be the method to call in VBA?
It's definitely possible. It would have to be a public sub or function and the form would have to be loaded at the time.
Forms("FormName").SubOrFunction