Custom Ribbon onAction syntax question
You should use the ribbon element's tag
property to store some values you want to pass to your action.
For instance, say you have a simple ribbon containing a few buttons:
- the first button uses a generic action
ribbonOpenForm
that opens a formFormDashBoardFinance
when clicked. - the second button uses a generic action
ribbonDoAction
that execute theLogOff("bye")
VBA function (not a Sub!) that, for instance, displays a message to the user and logs off. - the last one duplicates the behaviour that you wanted for your
fncMyFunction()
.
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="ribbonLoad" loadImage="ribbonLoadImage"> <ribbon startFromScratch="false"> <tabs> <tab id="Home" label="Home"> <group id="gpDash" label="Dashboards"> <button id="btHomeFinance" label="Finance" imageMso="BlogHomePage" onAction="ribbonOpenForm" tag="FormDashBoardFinance"/> <button id="btLogOff" label="Log Off" imageMso="DatabasePermissionsMenu" onAction="ribbonDoAction" tag="LogOff('bye')"/> <button id="btMyFunc" label="My Function" imageMso="AppointmentColorDialog" onAction="fncMyFunction" tag="'a string argument', 1234"/> </group> </tab> </tabs> </ribbon></customUI>
The VBA to manage the ribbon would be in a module:
Option Compare DatabaseOption Explicit' We keep a reference to the loaded RibbonPrivate ribbon As IRibbonUI'-----------------------------------------------------------------------------' Save a reference to the Ribbon' This is called from the ribbon's OnLoad event'-----------------------------------------------------------------------------Public Sub ribbonLoad(rb As IRibbonUI) Set ribbon = rbEnd Sub'-----------------------------------------------------------------------------' Open the Form specified by the ribbon control's Tag.'-----------------------------------------------------------------------------Public Sub ribbonOpenForm(control As IRibbonControl) DoCmd.OpenForm control.tag, acNormalEnd Sub'-----------------------------------------------------------------------------' Perform the action specified by the ribbon control's Tag' Use single quotes to delimit strings, they will be expanded.' The action to be performed must be defined as a public Function!'-----------------------------------------------------------------------------Public Sub ribbonDoAction(control As IRibbonControl) Dim action As String action = Replace(control.Tag,"'","""") Eval actionEnd Sub'-----------------------------------------------------------------------------' fncMyFunction example implementation' Use single quotes to delimit strings, they will be expanded.'-----------------------------------------------------------------------------Public Sub fncMyFunction(control As IRibbonControl) ' Split the string to separate the paramaters in the Tag Dim params As Variant params = Split(control.Tag, ",") ' Now we can assign each parameter Dim myString As String Dim myInt As Integer myString = Replace(Trim(params(0)),"'","") ' remove single quotes myInt = CInt(Trim$(params(1))) ' We're expecting an Integer ' ... do something with the params ... Debug.Print myString ' Will print: a string argument Debug.Print myInt * 2 ' Will print: 2468End Sub
An excellent resource for the Access Ribbon is Avenius Gunter's Access 2010 Ribbon site