Professional Documents
Culture Documents
Create Your Own Ribbon in Microsoft Access: Relational Database Design
Create Your Own Ribbon in Microsoft Access: Relational Database Design
You need Microsoft Access to perform the steps described in the tutorial. The steps have
been verified in Microsoft Access 2007, 2010 and 2013.
Extensible Markup Language (XML) is a markup language that defines a set of rules for
encoding documents in a format which is both human-readable and machine-readable. It is
defined by the W3C's XML 1.0 Specification[2] and by several other related specifications,[3]
all of which are free open standards.[4]
The design goals of XML emphasize simplicity, generality and usability across the Internet.[5]
It is a textual data format with strong support via Unicode for different human languages.
Although the design of XML focuses on documents, it is widely used for the representation of
arbitrary data structures[6] such as those used in web services.
Several schema systems exist to aid in the definition of XML-based languages, while many
application programming interfaces (APIs) have been developed to aid the processing of XML
data.
http://en.wikipedia.org/w/index.php?title=XML&redirect=no
USysRibbons table
It is possible to create a ribbon in various ways. The first option is to use the System table in
which the info of the ribbon is defined and stored. De second option is to build a ribbon
completely from 'VBA-code'. This section will focus on option one where we use the USys
table technique. This can be done in Access 2007, 2010 and 2013 ....
To create your own ribbons in Access you need to implement certain mandatory elements in
your database. The first object that is mandatory is the USysRibbons table. Follow the next
steps to create this table. NOTE: naming conventions and datatypes should be exactly the
same for a working ribbon!
ID Autonumber Keyfield
RibbonXML Memo
Access 2013 does not have the datatype Memo and Text. Instead choose Short
Text and Long Text that will do the trick!
4. Save the table (and if not done so) call it: USysRibbons
5. Because the table has a prefix Usys Access will 'see' this table as a system table. By
default the Access settings are not to show system tables. The average user will not see
this table. For now we do need to see the table so let's make them visible.
6. Show system tables: (rightclick on mouse)
XML Default Template
RibbonXML Basics
In the UsysRibbons table we created earlier we are going to add records with the values we
need for our ribbon. Every record in this table can be a seperate ribbon in Access. We will
start of with the creation of one ribbon just to get the basics right first.
For the first steps we are going to use good old Notepad. Of course you will find various XML
editors on the web but how conveniant is it that you actually don't have to dowload
something first to get started! So for now open Notepad and take a look at the XML lines
below. Those are the lines we are going to use. Make sure you typ the lines instead of just
copying them. By typing them yourself you will get a much better feeling of what you are
accomplishing. However you are free to copy it if you want to.
Where applicable i will tell you about the differences between the different versions of Access.
As you might have noticed the famous Office Button has disappeared in the 2010 and beyond
versions of Access. From 2010 and on we have to deal with the Backstage option.
Every first line for constucting a ribbon starts with the same components they are:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
This line sets a reference to the Office 2007 version !!
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
This line sets a reference to the Office 2010 (and later) versions
<ribbon startFromScratch="true">
true refers to creating a new clean ribbon with no other ribbons to be shown. Only your ribbon
will show up. Set this to false and all default ribbons will show up as well. Your ribbon will be
added last by default.
o Hides all build in tabs including the [Add ins] tab.
o The Office button only shows the following options (2007):
New, Open, Save as, Access Options en Exit Access
<tabs>
This is where all information will be added to create your own ribbon. You will see this in the next
steps.
At the end all 'Open' tags should get 'Close' tags as well. That makes it easy to create a template for
this!
</tabs>
</ribbon>
</customUI>
Important: XML is case sensitive! so be sure to check the syntax for capitalization
where needed. I will point that out where needed!
Equally important: although it looks like a link to click on this link is just a reference
link and will not provide you with any additional information about creating ribbons.
It's a pointer for XML to check the XML. See image below
Open the database we created earlier, the one where you added the USysRibbon
table.
Open the table USysRibbons in datasheet mode because we are going to add a
record.
Typ (or copy) the following XML snippet in the field [RibbonXML]. The autonumber
field will set a number by deafult so we won't have to think about that. In the
RibbonName field typ MyRibbon
<customUI
xmlns="http://schemas.microsoft.com/office/2007/09/customui">
<ribbon startFromScratch="true">
<tabs>
<tab id="MyTab1" label="My Tab 1"/>
<tab id="MyTab2" label="My Tab 2"/>
<tab id="MyTab3" label="My Tab 3"/>
</tabs>
</ribbon>
</customUI>
After adjustments it can look like this (this is not mandatory!) See remarks at
the end of the section.
You can now close the table. To activate our own ribbon we have to close the
database first. Yes you do have to close the database once after creating your
records in the USysRibbon table.
But wait. Access doesn't know we have created our own ribbon yet so we have to
set some settings. You do so by clicking the File - Options. In the dialog that pops
up click the Current Database menuoption. Now look for the Ribbon and Toolbar
Options an set the combobox to MyRibbon.
Close the database again. Now start up your database once more and see if you have
created the magic we were looking for.
Result if your XML is correct
Very Important: when typing in the XML (preferred to copying) just typ everyting without
hitting the ENTER key because that will cause Access to go to the next record. Instead just
typ everything in one line. When finished you can make the adjustments.
Neatly readable: If you want to have the same output as shown in the image above then do
the following steps. After each section where you want an Enter just hit Ctrl+Enter to go to a
new line in the record. This will give you the same layout as shown in the image above.
Copying and pasting will not give you the same layout. Although optically it looks the same
you will have major 'spaces' which need to be removed first!
In the image above it shows that a ribbon consists of quit a lot of elements. Not only Access
but also in the other Office Apps. That's important to know because once you have written
some XML for Access adding a ribbon to Excel or Word is childsplay. All elements have their
own purpose. If you want to create your own ribbon in Access you should know how these
are created in the XML. Let's have a closer look at the various elements.
1. Backstage (Office Button in 2007): A story of its own. This is the place where you'll find
the options for setting application variables. In the XML it's important to know which
version you are creating a ribbon for. If you want to 'edit' the backstage tab you'll have
to set the namespace to the 2010 version as described earlier.
2. The Quick Access Toolbar. Another specific element in the ribbon. The place for the
'regular' user to store options to modify his own mini ribbon. If you are building a
database you might want to 'hide' this option.
3. Tab: If we are talking bout a ribbon we are actually talking about a tab. A ribbon exists
of various tabs which have their own optiosn to choose from. An application can have
multiple tabs and multiple ribbons to choose from. During the exercise we are creating
new tabs with our own options.
4. Button: In a tab we can place various elements. One of the is the button element. A
button consists of a button and a button label. You can adjust the size of a button in
your XML. Clicking on a button can trigger a macro of default functionality.
5. Contectual tab: Contextual tabs appear when a certain object is active. In the image
above you see the contextcual tab "Design" active. Contextual tabs disappear once that
specific object is closed.
6. Group: Buttons that logiaclly belong together are being arranged in a logical group.
This is not a rule but is sure makes the tab logically build. Groups are being seperated
by Seperators (these are the vertical gray lines). A group has its own groupname to
identify the activity it is based on.
In this exercise we will add a button to our ribbon. To add a button to the ribbon we will have
to extnd our XML from our ribbon. We will add more options to our ribbon but we start of
with a button. Om een knop toe te voegen aan het lint zullen we de bijbehorende XML van
dat lint moeten aanvullen. We zullen het lint steeds verder uitbreiden maar beginnen dus met
de knop.
Take a look at the XML below. Notice anything? What you should notice is that every tab will
get its own ID. That's one of the rules you have to follow. This is also one of the pitfalls to
consider. Why? Because many objects look alike and you are tempted to copy and past that
specific code. In many cases peple forget to change the element id's when copy pasting and
that's where the ribbon will error out. So remember if you do copy and paste xml code don't
forget to change the id's and make them unique. That is mandatory!
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon startFromScratch="true">
<tabs>
<tab id="MyTab1" label="My Tab 1"/>
Add the line which has a border around it to your XML and save the record. Close and reopen
the database. What's the result?
Explanation: When you start creating a ribbon you will not notice the order of elements. If
you think back to the part where we explored the ribbon you can see that there is a specific
order to follow. In the above XML one important element is missing and that's the element
"Group". It makes no difference how many objects you place inside that group you have to
make one group at minimum. Take a look at any ribbon and you will see that all elements
are placed in a group even if it contains only one button. So we will the XML once more to
get the desired result.
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon startFromScratch="true">
<tabs>
<tab id="MyTab1" label="My Tab 1">
<group id="MyGroup1" label="My Group">
<button id="MijnButton1" size="large" label="Print Large"
imageMso="PrintDialogAccess"/>
</group>
</tab>
<tab id="MyTab2" label="My Tab 2"/>
<tab id="MyTab3" label="My Tab 3"/>
</tabs>
</ribbon>
</customUI>
Adjust the XML to the XML as shown above. Be careful and take a real close look at what has
changed! Close and reopen the database. What's the result?
Order in XML
After completing the exercise we have to conclude that there is one importance we have to
consider: the order! We need to place opening and closing tags where expected. So to
summarize: the order of XML elements are important. You can not add a button without
adding a group! So what do we need to create a ribbon? We need a ribbon, at least one tab,
at least one group and at least one button.
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon startFromScratch="true">
<tabs>
<tab id="MyTab1" label="My Tab 1"/>
<group id="MyGroup1" label="My Group">
<button id="MyButton1" size="large" label="Print large"
imageMso="PrintDialogAccess"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Every tag has a closing tag. The order of elements is: customUI, ribbon, tabs, tab, group,
button
PrintDialogAccess
FindDialog
Filter
UserRolesManage
Export
Instead of choosing the obvious choice for a small icon i would pick "small" instead of
"large". You will notice that XML doesn't understand the option "small". You will have to use
"normal" for a small icon!
Thus far we have created a ribbon, added a tab and a group and button. We can press the
button as long as we want but nothing will happen. So eventually this should lead to an
action being performed. To make this happen we have to take some action. First we have to
adjust the existing XML and secondly we have to write a macro in Access that performs the
actual action. We will alter our XML to create the option to perform the action. Next we will
add a simple macro in Access that we can trigger when the appropriate button is pressed.
The XML has to be extended with an additional attribute onAction. Behind the onAction we
write the name of the macro which we use in Access to perform the action. We will use our
earlier created XML and extend this with the onAction.
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon startFromScratch="true">
<tabs>
<tab id="MyTab1" label="My Tab 1"/>
<group id="MyGroup1" label="My Group">
<button id="MyButton1" size="large" label="Print Large"
imageMso="PrintDialogAccess"
onAction="MyCallBackOnAction"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Open Access and open your database where you are programming the ribbon in. Now open
the Visual Basic Editor window by pressing ALT-F11. Now choose Insert -> Module.
Next we will add the follwing subroutine. So either typ it or past code below in your screen:
When one of the buttons is pressed the identifier is passed as well. We have to pass this
identifier as well in our subroutine so Access knows which button was pressed. We achieve
this by adding a argument to our subroutine in which we tell Access which control has been
banging on our door. Take a look at the minor adjustments i made to the subroutine.
The argument (shown in blue) tells Access which button is pressed in the ribbon. Question;
what would be the value of the [control] that is passed to the Access subroutine? If we alter
the subroutine with the latest changes and restart the database again what will be the result
when we click the button?
Library References
You will see that you'll have one last step to take before everything works as expected. In our
subroutine we reference to the class IRibbonControl. This library is of by default in Access.
This will resolve into an error stating the macro can not be executed. So activate this library
by taking these final steps:
Go to the VBE (ALT+F11)
In the menubar click on Tools - References (below)
In the next dialog look for the appropriate library reference and click on the checkbox in
front of it
Now if you close the database and reopen it you can click the button. If all went well you
should see a messagebox appear!
The correct library references.
NOTE: for Office 2007 the reference is Microsoft Office 12.0 Object library... for 2010 it
is Microsoft Office 14.0
Anyone know what happened to 13.0?
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon startFromScratch="true">
<tabs>
<tab id="MyTab1" label="My Tab 1">
<group idMso="GroupFindAccess"/>
In the XML code (as shown above) we have added one line of extra XML.
<group idMso="GroupFindAccess"/> this line will take care of adding the functionality we
are looking for. Search and Find in Access. This group contains the regular functionality that
the build in tab also offers. If at all possible try adding as much buildin functionality as
possible. Your end users will thank you for it!
NOTE: the images were taken from Access 2013. If by any chance your ribbon doesn't work try
compacting and repairing your database. Just click the file button an you'll see a big button there!
In this exercise we are going to add build in functionality to our second tab in our ribbon. In
the previous section you read how to add this functionality to your own tab. Take a look at
the image below and try to recreate this in your own database.
One thing you have to know is the Group names to use when adding this functionality to
your XML. In the example the following default groups are used:
1. GroupPrintPreviewPrintAccess
2. GroupPageLayoutAccess
3. GroupZoom
4. GroupPrintPreviewClosePreview
I've created a report in the database. Nothing fancy just added it with the wizard. This doesn't
have to be a fancy report just one we can use to activate our ribbon. Open the report in print
preview and see the ribbon getting activated!
In previous versions of Access we had those nice menubars that could collapse. Of course you can do
this in your ribbon as well. It won't surprise you that you have to expand your XML code to achieve
this. So for now this will be our final exercise. We will use our database we have created and in our
third tab we will add a dropdown option with the various options to choose from.
We start of by opening the USysRibbons table and add lines to our RibbonName field:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon startFromScratch="true">
<tabs>
<tab id="MyTab1" label="My Tab 1">
<group id="MyGroup" label="My Group">
<button id="MyButton1" size="large" label="Print Large"
imageMso="PrintDialogAccess" onAction="MyCallBackOnAction"/>
<button id="MyButton2" size="large" label="Search Large"
imageMso="FindDialog" onAction="MyCallBackOnAction"/>
<button id="MyButton3" size="large" label="Filter Large"
imageMso="Filter" />
<button id="MyButton4" size="normal" label="User"
imageMso="UserRolesManage" />
<button id="MyButton5" size="large" label="Export"
imageMso="Export" />
</group>
</tab>
<tab id="MyTab2" label="My Tab 2">
<group idMso="GroupPrintPreviewPrintAccess" />
<group idMso="GroupPageLayoutAccess" />
<group idMso="GroupZoom" />
<group idMso="GroupPrintPreviewClosePreview" />
</tab>
<tab id="MyTab3" label="My Tab 3">
<group id="grpReports" label="Reports">
<button id="cdmRpt1" label="Added Work" size="normal"
imageMso="CreateReport" />
<button id="cdmRpt2" label="Open Invoices" size="normal"
imageMso="CreateReport"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Final result…..
NOTE: some xml has been word-wrapped. The imageMso should be directly after the
preceding lines of XML so don’t press enter in your code.