3

I have a custom ribbon that calls vba commands. When it does, I get the error message

Microsoft Access cannot run the macro or callback function [FUNCTION OR SUB]

where [FUNCTION OR SUB] is the Function or Sub name I am calling.

An example of the code that generates the above message is:

Public Function OnButtonPress2(ctl As IRibbonControl)

    MsgBox "OnButtonPress2"

End Function

Where OnButtonPress2 is called in the ribbon's XML file for a button as

onAction="OnButtonPress2

The above works for a macro, so it's likely not to be the ribbon's xml

======== VERSION 2 I've created a new simple test db with the XML and module shown below:

The results of each are:

Macro1 - works fine (it has a popup message)

CommandOnAction2 - msgbox "There was an error compiling this function"

OnButtonPress2 - msgbox "Microsoft Access cannot run the macro or callback function 'OnButtonPress2 '

on the line "Public gobjRibn As IRibbonUI", IRibbonUI does not autocomplete leading me to believe there is a reference that needs to be added

XML (Ribbon name is NewRibbon and is set to open on startup)

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="false">
<tabs>
<tab id="tab1" label="Your Custom Tab">

<group id="group1" label="Your Custom Group">
<button id="SampleButton" label="Macro1" onAction="Macro1" />
<button id="SampleButton2" label="CommandOnAction2" onAction="=CommandOnAction2()"  />
<button id="SampleButton3" label="OnButtonPress2" onAction="OnButtonPress2"  />
</group>
</tab>
</tabs>
</ribbon>
</customUI>

module (name is modRibbon)

Option Compare Database

Public gobjRibn As IRibbonUI

Public Function CommandOnAction2(ByVal ctl As IRibbonControl)
    MsgBox "Function CommandOnAction2"
End Function

Public Sub OnButtonPress2(ByVal ctl As IRibbonControl)
    MsgBox " Sub OnButtonPress2"
End Sub
4

1 回答 1

0

我能够在 Access 2010 中重新创建您的问题,并且通过将Functiona更改为Sub如下所示,我让您的示例正常工作:

Public Sub OnButtonPress2(ByVal control_ As IRibbonControl)
    MsgBox "OnButtonPress2"
End Sub

顺便说一句,在搜索有关此问题的信息时,我偶然发现了此处另一个论坛的帖子。在其中,Albert D. Kallal 讨论了使用onAction="=MyPublicFunctionName()"作为回调的替代方案;也许您会发现在某些情况下这很有用。

于 2013-05-14T08:34:15.560 回答