UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> Ribbon XML Control Codes    
Ribbon XML Control Codes

Introduction

The Ribbon was introduced into Microsoft Access with Office 2007. It has gained a reputation, especially for those used to the old style menus, of being difficult to work with, difficult to write (without specialist ribbon XML editors), difficult to read and extremely unforgiving of errors. The ribbon XML must be “well formed” which basically means it has to be well nigh perfect, even errors in capitalisation are not tolerated i.e. getVisible=”CallBackGetVis” is OK while GetVisible=”CallBackGetVis” is not.

Each ribbon is stored in a (nominally) standard Access table (with the name USysRibbons) in a Memo field, which is limited to 32K characters, which if you define long string fields (e.g for screentips), can quite quickly limit the number of commands you can place on each ribbon.

The approach here is to provide a standard ribbon command structure which can be used for all ribbon commands by simply copying and pasting the XML line and amending the control’s ID.

This is not a discussion of how to write and structure ribbon XML (there are already various websites (for example Gunter Avenius’ excellent website) that already do this extremely well). It is just an approach where the XML complexity is shifted to the VBA code (termed a callback) used by the ribbon. Each XML control is identically structured (for each control type) and any changes to the control function can be implemented in VBA code rather than by changing the XML.

Implementation

Each XML command is structured to provide a Control type, an ID, and various attributes and callbacks dependent on the particular control type. Rather than specify each of the attributes and callbacks specifically for each control within the XML, this approach uses common callbacks using the same VBA code for each callback type. The VBA code then uses the Control ID, passed as an argument, to process the callback.

Take for instance the XML for a button command

CODE

<button
   id="ButtonID"  
   label="Some Label"
   Screentip="Some Screentip"
   getVisible="ButtonIDSetVisibleCallback"
   getEnable="ButtonIDSetEnabledCallback"
   imageMso="GoToNewRecord"
   onAction="FunctionName"  />

This can be replaced for ALL your button commands with:

CODE

<button
  id="FunctionName"  
  getLabel="fGetLabel"
  getScreentip="fGetScrnTip"
  getVisible="fGetVisible"  
  getEnable=” fGetEnabled”
  getImage="fGetImage"
  onAction="fRbnAction"  />

only the id needs to be changed. The ID itself can be the name of the VBA function to be run when the control is clicked (See below).

In the callback functions, a Select Case statement is used to resolve the control:

CODE

Sub fGetScrnTip(ctrl As IRibbonControl, ByRef Screentip)
On error goto err_proc
    Select Case ctrl.ID
    Case “FunctionName1”
        Screentip = “Some screen tip”
    Case “FunctionName2”
        Screentip = “Some other screen tip”
   End Select

exit_proc:
   Exit Sub

err_proc:
  Resume exit_proc

End Sub

This same structure can be used for all callbacks, (excepting the OnAction callback), e.g.

CODE
Sub fGetVisible(ctrl As IRibbonControl, ByRef Visible)
On error goto err_proc
Dim bolVis1 as Boolean
Dim bolVis2 as Boolean
   ‘Set up visible status criteria
   bolVis1 = Some Criteria
   bolVis2 = Some Criteria

    Select Case ctrl.ID
    Case “FunctionName1”, “FunctionName2”
        Visible = bolVis1
    Case “FunctionName3”, “FunctionName4”
        Visible = bolVis2
   Case Else
         Visible  = True
   End Select

exit_proc:
   Exit Sub

err_proc:
  Resume exit_proc

End Sub

The OnAction callback is a special case, the ID is set to the name of the function to be called by the command. Arguments are passed to the function by adding them separated by (in this case) underline characters “_” to the ID. e.g. id=“fRunBook_2_3”

CODE

Sub fRbnAction (ctrl As IRibbonControl)
On Error GoTo err_proc
   
   Dim stAr() As String
   Dim strFunc As String
   Dim i As Byte
   
   Debug.Print "fRbnAction:  " & ctrl.ID
   ‘Add any common code,  e.g. SaveRecord

   stAr = Split(ctrl.ID, "_")
   If UBound(stAr) > 0 Then
       For i = 1 To UBound(stAr)
           strFunc = strFunc & stAr(i) & ", "
       Next i
       strFunc = Left(strFunc, Len(strFunc) - 2)
   End If
   strFunc = stAr(0) & " (" & strFunc & ")"
   Eval strFunc
   
exit_proc:
   Exit Sub

err_proc:
  MsgBox "Error in Function: ' fRbnAction '" & Chr(13) & Err.Description
  Resume exit_proc

End Sub

Hence a ribbon control ID of “fRunBook_2_3” would result in the equivalent VBA call:
Call fRunBook (2, 3)

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 7,522 times.  This page was last modified 01:14, 25 August 2012 by Jack Leach. Contributions by Pere_de_chipstick  Disclaimers