Customizing the New Access UI

I described the new Access UI in this post and this post way back in March.  Now I'll run through how you program the ribbon UI using VBA.  In a future post (we're still working out the content) I'll do another post on how to do the same thing using macros (so the app will run in disabled mode). 

Customized Ribbons

The ribbon UI presents a ton of new flexibility for us creating Access and for you building apps on top of Access that wasn't there before.  As described in the posts on the ribbons, there are a lot of new control types that can be placed in the ribbon, and there's much more flexibility about how they are presented.  The downside to this additional flexibility is that the ribbons are more complex to create than the old command bars.  Building ribbons isn't beyond the reach of any successful Access user today, but it will take some more work.  The upside is that the end product can be significantly better.

Here's an example of an application with a customized ribbon UI:

(Click image to enlarge)

The app has a rich set of controls, including a dropdown to let different users log on:

And of course after each user logs on, you can fire code to both update the contents of the ribbon itself and to take actions inside the application.  Here is an example of the changes to the ribbon itself:

(Click image to enlarge)

The application can have multiple tabs, just as Access does, and the user switches between them just as in the regular Access ribbons.  Here's the Form ribbon:

(Click image to enlarge)

In addition, custom ribbons can use the full range of controls available in Access.  Here's an example of a dropdown button on the Reports tab:

And an alternate split button presentation for the same data:

Controls on the ribbons can how have "Super Tooltips" (not sure if that is the real name?).  These are extra large tooltips containing more, and richer information than the command bar style tooltips, but that launch in just the same way.

Customizing the Ribbon

There are 5 steps to customizing the ribbons:

  • Create the ribbon XML - the ribbon definitions are done through an XML file, and the first step is to build that file.  This is pretty easily done with a simple text editor, but can be made even easier with Visual Studio tools.
  • Specify callbacks - next, you need to build small VBA routines that handle ribbon actions like clicking a button or selecting something from a dropdown.
  • Create table for XML file - next, you create an Access table to hold the XML you created in step 1.
  • Add the ribbon to that table - next you create a record in the table for each custom ribbon, containing the ribbon name and the XML definition of the ribbon.
  • Set the Custom Ribbon property - finally, you set the Custom Ribbon property for the database as a whole or for specific forms and reports to load the ribbon you defined in the table above. 

Creating the XML Ribbon Definition

The ribbon definition is a simple XML file and can be easily created in any text editor, but if you're using Microsoft Visual Basic.NET 2005 Express Edition or Microsoft Visual Web Developer 2005 Express Edition, you can get IntelliSense for your code.  Visual Web Developer looks something like this:

(Click image to enlarge)

You can point it at the custom ribbon schema at http://schemas.microsoft.com/office/2006/01/customui and VWD should pick up the correct file, which is called customUI.xsd.  Alternately, you can download the file from http://officeblogs.net/UI/customUI.xsd

(Click image to enlarge)

This enables VWD to provide type-ahead IntelliSense for the correct ribbon xml.

(Click image to enlarge)

You can see the XML file for the sample application we're building by clicking here.  The contents of the file are:

  1. customUI - this is the top level element for a custom ribbon
  2. ribbon - defines the ribbon itself.  Set the startFromScratch attribute = "true" to create a new blank ribbon.  If this is omitted or left blank, the custom ribbon will merge with the existing Access ribbons.
  3. tab - creates a new tab in the ribbon
  4. group - creates a group (or "chunk" in my ribbon description post).  These are used to logically group controls in the ribbon.
  5. id - unique name of a control in the ribbon
  6. label - static text displayed with a control
  7. button - similar to a command button.  Use the onAction attribute to specify the name of a subroutine in the database which will be called when the button is clicked.
  8. dropDown - creates a dropdown list that cannot be updated.  To create a drop down that the user can type in, use a comboBox control instead.
  9. imageMSO - specifies the name of a built-in control in Office that can be used as in icon in your ribbon.

Creating VBA Call-backs

Next, you need to write the routines that are called by the ribbon when an action is performed.  Here's what to keep in mind when writing these routines:

  • You'll need a reference to the Microsoft Office 12.0 Object Library
  • Arguments to Sub routines are fixed
  • In many cases, you can use a single routine for many events.  For example, you might have a single subroutine that handles opening forms in your database using button controls in the ribbon.  The tag attribute in the button can be used to store the name of the form, and the generic routine simply opens that form name.  For example, such a routine might look like this:

Public Sub onOpenForm(control as IRibbonControl)

    ' Open the form which is specified in the tag attribute

    DoCmd.OpenForm control.Tag

End Sub

  • To refresh the ribbon, call the Invalidate method on the IRibbonUI object.  This object is passed to you in the onLoad callback when the ribbon is loaded.  You can save an extra copy of this object to use as needed:

' Cached copy of the RibbonUI. Used to invalidate the ribbon to

' refresh controls.

Public gobjRibbon As IRibbonUI

 

' customUI onLoad event handler

Public Sub onRibbonLoad(ribbon As IRibbonUI)

    ' cache a copy of the Ribbon so we can refresh later

    Set gobjRibbon = ribbon

End Sub

 

' Then, to refresh the entire ribbon, call:

gobjRibbon.Invalidate

You can look at the macros in this example database to get a better idea of the code to write.

Creating the Ribbon Table

Simply create a table in your database called USysRibbons.  This table should have 2 columns:

  • RibbonName - type: Text(255)
  • RibbonXML - type:  Memo

The table should looks something like this:

Adding the Ribbon XML

Now simple create a record for each ribbon you'd like to create in the table above.  Note that this is already done in the image above.

Set Custom Ribbon Property

Next, you go to the Access Options dialog and under Toolbar Options, choose the ribbon for the selected object.  Again, you can see these settings most easily in the attached database. 

Tips, Tricks, and Additional Information

Here are a few general things to remember:

  • If you use VBA callbacks, the database must be opened in enabled mode for the code to run.  You may receive an error in Beta 2 when opening a database with custom ribbons in disabled mode.
  • Set a reference to the Office 12.0 Object Library to use IRibbonControl.  You'll get a compile error if you don't have the reference included.
  • Elements and built-in names of controls and images may change prior to release.
  • To edit the ribbon, and XML editor such as Microsoft Visual Basic.NET 2005 is quite handy.  The Express Edition works great.
  • Show system objects in the nav pane to modify the USysRibbons table
  • You can hold down the SHIFT key when opening the database to open it with the Access ribbon rather than your custom ribbon.  However, this doesn't work in the Beta 2 build (sorry about that).
  • You can show errors in the UI XML by turning on an option in Access.  If you open the Office menu (the round button), and select Access Options / Advanced  and then check "show user interface errors", Access will show a dialog describing any problems in the Ribbon XML.  The dialog will look something like this: 

         

For additional resources take a look at these sites:

Next Time

The next post will be the first of several drilling down into using Access in conjunction with Windows SharePoint Services.

 

Office Blogs Comments

Comments: (46) Collapse

  • Wow. Looks very cool. I'm going to try to use it in my app. Should be interesting...

  • Is the ribbon UI implemented by using the the IE rendering engine, the way some parts of the Outlook UI? It worries me if it is, as it means that Access then becomes subject to any security flaw or bug of any other kind in the IE rendering engine. Please tell us that the IE rendering engine is not used for presenting the ribbon!

  • Is runtime going to include the necessary Office Lib?

  • The ribbon rendering UI is an internal office library that is unrelated to the IE rendering engine. >> Is runtime going to include the necessary Office Lib? Sure. This is pretty fundamental to building an app.

  • For those of you who want to completely get rid of the ribbon--here are the steps: 1. Create a new table called USysRibbons.

    2. Add two fields RibbonName (text) and RibbonXML (Memo).

    3. Create a new record with the name "Blank" (doesn't really matter what you call it). Also add the following XML: 4. Restart your database.

    5. Go into the Office Button | Access Options. Click on the Current Database.

    6. In the Toolbars Option set the Custom Ribbon Id to Blank (name in step 3).

    7. Restart the database.

  • I'm frankly more concerned with the new object task pane. I would like to be able to display the objects in the database in a manner similar to the old database window so that we can see more objects without having to scroll down vertically as much. As an example, I have a database that has 109 queries, 80 tables, 22 forms, 30 reports 18 modules, and several DAPs. Navigating these objects using the current pane paradigm is somewhat of a pain (even with grouping option). The problem could be solved if the current pane just provided the means to repint the object pane to allow the objects to form new columns when the pane is resized to the right. Resizing the pane now has no effect as far as the contained objects are concerned objects are concerned. It seems to me that as one resizes the pane to the right the vertical scroll bar should disappear and the objects should just appear in several rows as the view pane gets resizes to take up more of the screen. This would make devolopment much easier when working with many different objects.

  • Microsoft: There are 5 steps to customizing the ribbons: Create an XML File and BLA, BLA, BLA... Me: I thought Access was a RAD! RAD: Rapidity Application Developement... I hope Access 12 supports still the old toolbars!

  • How are toolbars & buttons controlled by code? How do you add a button to an existing toolbar using VBA? Can you add/remove and enable/disable buttons and menu items at runtime any more easily than with current versions? If you can still create toolbars and menus using VBA then it would probably be more convenient than the remarkable 5-step XML process you give above (admittedly the call-back routines would be required in any case).

  • >> Navigating these objects using the current pane >> paradigm is somewhat of a pain (even with grouping option). Thanks for your feedback on the navigation pane Ken. Have you discovered the search bar? Internally, we have a db that we use for hours a day every day. It is about the size of your example and growing weekly. The people using the database have found the search bar far more useful than visually scanning through a large list of stuff. We are looking into improving the keyboard model so that it is easy to get focus into the control and quickly into the details section of the pane. >> I hope Access 12 supports still the old toolbars! The old toolbars OM is still supported. If you customized your toolbar using Access 2003 those customizations still apply. If you just extended our existing menus the commands get placed in the Add-Ins tab. If you have replaced our built in tool bars then your app should run without the ribbon. We don't support UI modifications of legacy menus and command bars. >> How are toolbars & buttons controlled. There is a new method called LoadCustomUI that takes a XML string. You add callback attributes (such as getVisible, getEnabled, and so forth) to the elements in your Ribbon XML that you want to change. Here is a list of all callbacks msdn2.microsoft.com/.../ms406047.aspx. The following paper provides a pretty good overview with lots of details about Access specific details

    msdn2.microsoft.com/.../ms406046.aspx. I started working with ribbon customization last week and was surprised how easy it was to pickup. The syntax of the markup language is straight forward especially with a basic XML editor that provides type ahead. We will provide more and more icons, code and ribbon samples that should help get people started very quickly. The model isn't perfect and could be easier for beginning developers; however, it does provide a rich system for building apps optimized for efficiency and discoverability.

  • Customizing Ribbons in Access 12 it is too much complex. We need a "Visual Editor" to do it... I have not understood if the icons must reside externally or they can be included in the FE. IMHO the matter of ribbons will be an enormous problem for Access Developers. Access 2003 "forever"? ;-) Bye P.S:

    Setting reference to the Office library I have had many problems of "Lost References" with old Runtimes Edition of Access!! The only solution: www.sagekey.com

  • Thank you for sharing with us your knowledge. Would be possible to have a sample of your database available? Thanks, Mauro

  • Zen,

    I agree with you that a visual editor would be very cool--that was just something that couldn't get done in this release. I'm guessing third-parties will fill the gaps until we do something more native. I think you might be surprised how easy it is to pull together--especially with a few examples. You simply import the USysRibbons table and start tweaking the Label, OnAction and msoImage attributes. When the next beta is released we will post a sample database that has rich ribbons and doesn't require any code--just XML markup. The images are pulled from the Office library of images. There will be help documentation in the next beta that provides a list of all the available icons. You can load your own images from attachments with a few lines of code. We will have sample code available when folks have a build where it will work. Mauro,

    The sample database is provided in a link above... clintc.officeisp.net/.../RibbonXBlogDatabase.accdb

  • Clint C: "You can load your own images from attachments with a few lines of code" The icons must reside externally, they cannot "embedded" in the FE?

    I hope you understand that would be a very large limitation... We recommend you not underrate the problems of Runtime distribution: the "lost references" and other problems of the package wizard are much serious.

    We don't want to acquire the sagekeys scripts for all the life : -) Access without a rock solid runtime distribution, is a product that has no matter. Bye

  • Hi chaps/chappettes I see that the icons are from the Office library and that you cannot embed them. Do they work differently from the OLE wrapper objects that previous version use? I'm just wondering if I must ship custom icons with the application in an install package (to store them externally on the client's computer) or if they could be treated as OLE objects (or better still, BLOBs)? DM

  • >>Have you discovered the search bar? Have not really used it yet. Will take a look at it tonight at home. Thanks for the suggestion clint! Ken

1 2 3 4  Next >
Comments

Comments: (loading) Collapse