Sue and I were talking the other day about Ribbon customizations with regard to some training materials that she is preparing for our support engineers. Along the way I mentioned command repurposing and how you could do some pretty cool stuff with it in Access applications. She suggested that this might make a good blog post and I think she's right.I've been working on an application to track the work that I do throughout the year, and thought I would add a database password to encrypt the database. Since Access has this command built in on the Ribbon, I could use the idMso attribute of a button set to SetDatabasePassword to add this command to my database. Cool - however,
Microsoft Office 2007 Enterprise, adding a database password requires that the database is opened exclusively, and I didn't really want to see the built-in error message with the built-in command. Enter command repurposing.Repurposing a command refers to the ability to use a built-in command in the Ribbon, but to provide your own functionality with that command. In my scenario, I wanted Access to encrypt the database, but I want my own error message when the database is not opened exclusively. To do this is actually quite simple. In the XML for the customization, specify the command that you want to repurpose using the command element. Then, specify the control that you want to use (in this case, the SetDatabasePassword button) in a Ribbon.<customUI xmlns="">
<commands>
<command idMso="SetDatabasePassword" onAction="OnSetPassword"/>
</commands>
<ribbon>
<tabs>
<tab id="tabTab1" label="Password">
<group id="grpGroup1" label="Password">
<button idMso="SetDatabasePassword" size="large" label="Set Password"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Add this XML to a USysRibbons table and set the RibbonName property of the database to match the entry in the table.The onAction callback of a repurposed command is slightly different from that of a regular control in that it allows you to indicate whether or not to cancel the default action of the built-in command. Add the following code for the OnSetPassword callback routine:Public Sub OnSetPassword(control As IRibbonControl,
Windows 7 Ultimate, ByRef CancelDefault)
If (CurrentProject.Connection.Properties!Mode = 12) Then
CancelDefault = False
Else
MsgBox "You must open the database exclusively to set the database password.",
Windows 7 Professional, _
vbExclamation, "Cannot Set Password"
CancelDefault = True
End If
End SubIf the database is opened exclusively (Mode=12), we'll allow the default action to continue by setting the CancelDefault parameter to False. If the database is not opened exclusively,
Windows 7 Pro, we'll stop the built-in action by setting it this argument to True. To try this out,
Office 2007 Serial, click the button in the new tab that was added by the customization.Thanks Sue for the suggestion! <div