Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 29: Customizing Access Ribbons


1045


Managing Ribbons


From time to time, you may find it necessary to replace a ribbon with another one while the user
works with an application. So far, all the examples you’ve seen are loaded as Access starts and stay
on the screen as long as the user works with the application. We haven’t yet covered the steps
involved in closing one ribbon and opening another.


You don’t really close a ribbon. Instead, you invalidate it, causing Access to discontinue managing
the ribbon. The syntax for invalidating a ribbon is


Ribbon.Invalidate

The problem is knowing which ribbon to invalidate. Microsoft suggests you cache a reference to a
ribbon each time it is opened, and then use that reference to invalidate the ribbon should the need
arise. You may have noticed the onLoad attribute of the prototype ribbon in the section titled,
“The Basic Ribbon XML,” earlier in this chapter:


<customUI xmlns=“http://schemas....” onLoad=“onRibbonLoad”>

(Some text has been removed from this statement for clarity’s sake.)


The onLoad attribute specifies a callback that supports the ribbon’s startup activities. You can use
the onLoad callback to cache a reference to the ribbon.


Begin by establishing a public object variable that will point to the ribbon:


Public gobjRibbon As IRibbonUI

Next, use the object variable to store a reference to the ribbon during startup:


Public Sub onRibbonLoad(ribbon As IRibbonUI)
‘Cache a copy of the Ribbon:
Set gobjRibbon = ribbon
End Sub

The gobjRibbon object variable remains in scope while the application is used. If you need to
invalidate the ribbon, simply invoke the Invalidate method:


gobjRibbon.Invalidate

Of course, after you invalidate a ribbon, you want to replace it with another. Invalidating a ribbon
does not remove it from the screen. Unless you overwrite it with another ribbon, it stays on the
screen, taking up space, but not really doing anything.


Use the Access Application object’s LoadCustomUI method to load a different custom ribbon:


Application.LoadCustomUI( _
CustomUI_Name As String, CustomUI_XML As String)
Free download pdf