My.ADVISOR.com Sign-In
ID
Password

Member Center / Sign-Up
Go to Article
Advanced Search 

ADVISOR TIPS

Protecting Command Bars and Menus

Keep Microsoft Access users from closing or moving your command bars and menus with this technique.

Willem Holleman


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.

What do YOU think about this topic? Share your advice and thoughts using this form.

Your Name

REQUIRED : PUBLIC

Your E-Mail

REQUIRED : PRIVATE

Job, Company

OPTIONAL : PUBLIC

City, State, Country

OPTIONAL : PUBLIC

Your Web Site

OPTIONAL : PUBLIC

Your Comment

Please help everyone by keeping your comments on-topic, using clean language, and not defaming or making personal attacks.


Your e-mail address is required, but it will not be displayed to the public or given to anyone. See our Privacy Policy. Comments become visible after they pass our spam filter, and spammers and abusers are permanently blocked. Please report spam or abuse.

Ken GetzTechnical 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

Printer-friendly
page layout

Keyword Tags: Database, Database Development, Database Management, Microsoft, Microsoft Access

ADVISORAMA
Teach a child to be polite and courteous in the home, and, when he grows up, he will never be able to edge his car onto a freeway.

ARTICLE INFO

DataBased Advisor

Web Edition: 2005 Week 15, Doc #16406

FREE ACCESS FREE ACCESS

SUBSCRIPTION STATUS
You 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.

Subscribe to FileMaker Advisor Magazine

Read the advanced guide to creating custom business database solutions with FileMaker software. Subscribe now to gain access to all the archives and downloads.

FileMaker.Advisor.com

Subscribe to Advisor Basics of FileMaker Pro

Learn the fundamentals of using FileMaker Pro software. Every issue gives you step-by-step instructions on creating the databases you need. Subscribe now!

FileMaker.AdvisorBasics.com

Showcase Your Smarts

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

AdvisorTips.com

Use of this or any other site, content, product or service of Advisor Media constitutes acceptance of Terms of Use.
Portions copyright ©1983-2008 Advisor Media, Inc. All Rights Reserved.
Reuse or reproduction of any portion or quantity of Advisor Media's copyrighted content, in any form, for any purpose, requires written permission.
ADVISOR®, the ADVISOR logo, and other names and logos that incorporate ADVISOR are registered trademarks, trademarks or service marks of Advisor Media, Inc. in the United States and/or other countries.
Other trademarks are used for identification, editorial or descriptive purposes and are the property of their owners.
Hosted by Prominic.NET Website powered by
LOTUS SOFTWARE
GETZK130-01 posted 03/01/2007 modified 12/03/2008 03:36:16 AM ztdbms/ztdbms
domino-144.advisor.com my.advisor.com 12/03/2008 03:21:20 PM