|
|
ADVISOR TIPS
Protecting Command Bars and Menus
Keep Microsoft Access users from closing or moving your command bars and menus with this technique.
I like to include custom command bars with my applications -- users like to have menu and toolbars pertinent to the applications that I create for them -- but users have a bad habit of moving, hiding, and otherwise making their own lives more difficult. To prevent such actions, I started protecting the command bars in my applications, making sure users don't close or move them. It's easy to do with VBA code.
You only have to run the code once to protect the command bars. You might want to simply run this before you distribute the application. Or, you might want to provide a mechanism whereby users can protect and unprotect the items at will, making it easier to control the behavior.
To work with command bars programmatically, you must first select the Tools > References menu from VBA to set a reference to the Microsoft Office <xx> Object Library, where <xx> corresponds to the version of Office you're using. Then, create a standard module, and add these two procedures:
Sub ProtectCommandBars()
Dim cbar As CommandBar
For Each cbar In Application.CommandBars
If Not cbar.BuiltIn Then
cbar.Protection = _
msoBarNoMove Or msoBarNoCustomize Or _
msoBarNoChangeVisible
End If
Next cbar
End Sub
Sub UnprotectCommandBars()
Dim cbar As CommandBar
For Each cbar In Application.CommandBars
If Not cbar.BuiltIn Then
cbar.Protection = msoBarNoProtection
End If
Next cbar
End Sub
Then, to protect your command bars, call the ProtectCommandBars method; to unprotect, call UnprotectCommandBars. If you only want to do this once, you can call either method from the Immediate window. If you want to let users make the choice whether to protect the command bars, call the procedures from menu items. In any case, I've found these procedures useful in ensuring my applications work the way I intended.
-- Willem Holleman, Leiden, The Netherlands
I've fought with command bars for years, and never noticed this feature! Thanks for pointing it out. Being compulsive about reducing code size, however, I feel compelled to provide a version of the code that merges the two procedures. I'm not sure it's any fewer lines of code, but it does let you provide the same functionality without having to maintain two separate procedures.
Here's my slightly modified version:
Sub HandleCommandBars(Protect As Boolean)
Dim cbar As CommandBar
Dim value As MsoBarProtection
' Calculate the value to store into the
' Protection property:
If Protect Then
value = msoBarNoMove Or _
msoBarNoCustomize Or msoBarNoChangeVisible
Else
value = msoBarNoProtection
End If
' Now loop through all the command bars,
' looking for ones that aren't built in.
For Each cbar In Application.CommandBars
If Not cbar.BuiltIn Then
cbar.Protection = value
End If
Next cbar
End Sub
Call the procedure passing in True or False to protect or unprotect your command bars:
' Protect:
HandleCommandBars True
' Unprotect:
HandleCommandBars False
Whether you go with the original procedures or this combined procedure, you can finally keep users from undermining your careful work.
-- Ken Getz, Access-VB-SQL Advisor Magazine technical editor
This tip was published in the December 2004 issue of ACCESS ADVISOR Magazine. Subscribers can get more Advisor Tips, Advisor Answers, and how-to advice on succeeding with Microsoft Access technology at www.MSAccessAdvisor.com.
Technical Editor Ken Getz is a programmer, technical writer, educator,
and senior consultant with MCW Technologies. He's a lead courseware
author and presenter, he develops custom Windows and Web applications and tools using Visual Studio and Microsoft Office, and he's been a Microsoft MVP award winner since the program began. Ken is a frequent speaker at Microsoft events, ADVISOR SUMMIT, and other technical conferences. He's co-author of ASP.NET Developer's Jumpstart, Access Developer's Handbook series, and VBA Developer's Handbook. http://www.mcwtech.com/cs/blogs/keng
Keyword Tags: Database, Database Development, Database Management, Microsoft, Microsoft Access
ADVISORAMA If anyone comes to my yard and asks, "What kind of plant is that?" I say, "A dead one."
|
ARTICLE INFO
Web Edition: 2005 Week 15, Doc #16406
FREE ACCESS
|
SUBSCRIPTION STATUSYou are not signed-in. If you are a subscriber to this publication, sign-in above to access locked articles. To subscribe or renew go to www.AdvisorStore.com.
|

Get it all -- every current and past ADVISOR tech/business publication, now all-in-one subscription, with new articles and a huge reference library packed with expert advice, how-to and downloads. Subscribe now to get it all.![]() 
Read the advanced guide to creating custom business database solutions with FileMaker software. Subscribe now to gain access to all the archives and downloads.![]() 
Learn the fundamentals of using FileMaker Pro software. Every issue gives you step-by-step instructions on creating the databases you need. Subscribe now!![]()
![]()
![]()
 ![]()

Submit your tips, techniques and advice and let Advisor promote your business and build your career. Show the world what you know!![]()  ![]()
|
|