Microsoft Access VBA Macro Programming

(Tina Sui) #1
<group idMso="GroupQuickFormatting" />
</tab>
</tabs>
</ribbon>
</customUI>

One of the most important points to bear in mind is that XML is case-sensitive and you
need to follow the exact case when putting together your own XML; otherwise, it will not
work. Whereas in VBA this all gets taken care of via the editor, it is very important to get it
right here.
The XML starts off with a root element:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">

Elements are then defined for <ribbon> and <tabs>. The tabs are effectively the menu
items on the menu bar in Access. There is then a tab definition for the new menu item called
Contoso. This line provides it with an internal ID, the name as it will appear on the menu bar,
and where it will be positioned on the menu bar.
The next lines add in the ribbon groups forclipboardandfont. The third group added in
is a custom one with a custom internal ID and a label to define it. Buttons are then added in
as child elements, providing custom internal IDs, labels, sizes, and images to use. You can
see how all these parameters work by comparing what you see in theribbongroup on the
Access screen to the actual XML.
The custom group also has a closing element to finish it off: </group>. This defines that
the definition of the custom group has finished.
Curiously, three other groups are also defined after this, but their names do not exist in the
ribbon model, so they have been ignored.
The XML is then finished off with closing tags for tab, tabs, ribbon, and Custom UI.

Creating Code for Your Custom Buttons


As already mentioned, the buttons in the ribbon custom group only produce an error message
because they have no code to run. You need to create code that utilizes theOnActionparameter
from the custom ribbon control that was created.
To make your callback code work, you will need to add a reference in VBA to the Microsoft
Office 12.0 Object Library. To do this, click Tools | References in the VBE menu and scroll
down the list of libraries to Microsoft Office 12.0 Object Library. Tick the check box for this
and click OK.
Usefully, the Custom UI Editor assists you in doing this. This is one of the big advantages
of using it instead of trying to work directly with the USysRibbons table. If you look at the
toolbar displayed, the icon on the far right has a label of “Generate Callbacks” if you hover
your mouse over this. Click this and you will see skeleton code for all three custom buttons
within the XML. Open the VBA Editor window in Access and insert a module by clicking
Insert | Module. Make sure you insert it in the workbook that has the custom ribbon;
otherwise, it will not work.

136 Microsoft Access 2010 VBA Macro Programming

Free download pdf