Modifying the Built-In Menus and Toolbars


Modifying the Built-In Menus and Toolbars

If you work for a large company with some in-house programming staff, you may feel that the programming features built into Access are not something that all staff in your organization should have access to. This section shows how you can disable certain commands for all Access databases used by a single computer. You could run this VBA code after installing Access on a computer. If a user then requests the additional functionality, the same software with a slight modification will enable the commands that were disabled. This modification works out to be very effective in a database where Access to the VBA code is restricted to the developer or if the database is in MDE format. Of course, the same users could use VBA in another database to turn these menu bars on again, supposing that they actually could find out what was going on. If this possibility worries you, you will have to add the same code to the startup procedures of all your important databases. Though this may seem heavy-handed, remember that some organizations have corporate IT policies that assume that control of in-house development produces a better result than the empowerment of end users.

Note  

The built-in menus remain disabled even if you use the AllowByPassKey property to open the database.

To disable the toolbar commands, I have written a subroutine that disables the critical menu and toolbar items in the Access environment. This software is located in the VBA code behind the frmDisableMenuItems form (as shown in Figure 7-20). The sample restricts itself to the commands that are available from the Form View and Report Preview mode. Disabling commands has the effect of making the commands unavailable but still visible on the menus (see Figure 7-21).

click to expand
Figure 7-20: The frmDisableMenuItems form, which disables/enables developer commands.

Figure 7-21: The sample software, with disabled commands on the Access built-in menus.
Note  

When you change a built-in menu, you actually create a new Access menu called a global menu bar. This global menu bar now becomes the default menu for your computer, and the built-in menu isn't used any more. The same applies for toolbars and shortcut menu bars.

To disable the commands, I use a function that I have written (called EnableMenuItem ) that enables or disables a menu item by using the CommandBar object. As you will see in the VBA code that follows , I identify the menu commands by using the text names that appear on the built-in menus themselves . I have used this approach because it is far more self-explanatory. For one of the commands, I have resorted to using the numerical identifier for a toolbar because the built-in toolbar and shortcut menu both have the same names (Form View). I have one big apology here for anyone reading this text that uses a foreign language version of Access: The text names for these commands could be different on your computer. Anyway, here is the code that you can use to disable built-in menu items.

 Private Sub cmdDisableMenusItems_Click() ' Enable/disable all the command bars controls that a user could ' use to elude the protection in your database. On Error GoTo err_cmdDisableMenusItems ' To disable the toolbar commands, set the following constant to False. Const ENABLEITEM As Boolean = True enableMenuItem "File", "Get External &Data", ENABLEITEM    ' Access 97 caption. enableMenuItem "File", "&Get External Data", ENABLEITEM    ' Access 2000 caption. enableMenuItem "View", "&Design View", ENABLEITEM enableMenuItem "View", "&Toolbars", ENABLEITEM enableMenuItem "Tools", "&Relationships...", ENABLEITEM enableMenuItem "Tools", "Anal&yze", ENABLEITEM enableMenuItem "Tools", "Securi&ty", ENABLEITEM enableMenuItem "Tools", "Re&plication", ENABLEITEM enableMenuItem "Tools", "Run &Macro...", ENABLEITEM        ' Access 97 caption. enableMenuItem "Tools", "&Macro", ENABLEITEM               ' Access 2000 caption. enableMenuItem "Tools", "Start&up...", ENABLEITEM enableMenuItem "Tools", "ActiveX &Controls...", ENABLEITEM ' Access 97 caption. enableMenuItem "Tools", "Active&X Controls...", ENABLEITEM ' Access 2000 caption. enableMenuItem "Tools", "&Customize...", ENABLEITEM enableMenuItem "Tools", "&Options...", ENABLEITEM ' Caution: If you disable the database utilities option, that will remove ' the compact options. enableMenuItem "Tools", "&Database Utilities", ENABLEITEM ' To test that the menu item exists, use the following code: If Not enableMenuItem("Window", "&Unhide...", ENABLEITEM) Then    MsgBox "Problem with the Unhide Menu Item" End If ' Now disable the protection related buttons on the form and report toolbars enableMenuItem "Form View", "View", ENABLEITEM enableMenuItem "Form View", "&Design View", ENABLEITEM enableMenuItem "Form View", "&Database Window", ENABLEITEM enableMenuItem "Form View", "&New Object", ENABLEITEM enableMenuItem "Print Preview", "View", ENABLEITEM enableMenuItem "Print Preview", "Design &View", ENABLEITEM enableMenuItem "Print Preview", "&Database Window", ENABLEITEM enableMenuItem "Print Preview", "&New Object", ENABLEITEM ' For the shortcut toolbar, use the index of the toolbar for the ' actual name of "Form View," which is the same name used for toolbars. enableMenuItem 85, "Form &Design", ENABLEITEM ' For the shortcut toolbar, use the index of the toolbar for the actual ' name of  "Print Preview," which is the same name used for toolbars. enableMenuItem 95, "Report Desig&n", ENABLEITEM If ENABLEITEM Then    MsgBox "All protection related menus items on this computer are Enabled", _           vbInformation, "Built-In Menu Items Unprotected" Else    MsgBox "All protection-related menus items on this computer are Disabled", _           vbInformation, "Built-In Menu Items protected" End If Exit_cmdDisableMenusItems: On Error Resume Next    Exit Sub err_cmdDisableMenusItems:    Select Case Err.Number         ' Problems with unload process.       Case vbObjectError + 999          ' To see line immediately after the error line, press CTRL+BREAK,          ' drag yellow arrow to Resume Next, then presss F8.          Resume Next       Case Else          MsgBox "Error No. " & Err.Number & " -> " & Err.Description    End Select    Resume Exit_cmdDisableMenusItems End Sub 

In the enableMenuItem function that follows, the CommandBar object from the Microsoft Office Object Library is used. This function iterates through all the controls (we call them commands) in a particular command bar (toolbar) until the command we want is found. The Boolean value passed as an argument to the function enables or disables the menu command.

 Function enableMenuItem(varCBBarName As Variant, strCBarCtlCaption As String, _                         booEnabled As Boolean) As Boolean ' Turn menu items on and off. ' There is no error trapping in this subroutine. ' You should handle this in higher-level procedures. ' Caution: This function will disable a menu item if booEnabled = True. ' This procedure requires a reference to the ' Microsoft Office X Object Library. Dim cbrReq As CommandBar Dim ctlName As CommandBarControl Set cbrReq = CommandBars(varCBBarName) For Each ctlName In cbrReq.Controls    If ctlName.Caption = strCBarCtlCaption Then       ctlName.Enabled = booEnabled       enableMenuItem = True       GoTo enableMenuItem_Exit    End If Next ctlName enableMenuItem = False enableMenuItem_Exit:    Set cbrReq = Nothing End Function 
Note  

Because you may have trouble enabling a particular command, you can restore the toolbar to its installation settings. Open the Customize dialog, select the Toolbar tab, choose the toolbar, choose properties, and click the Restore Defaults button.

Now I will briefly review some samples that were not discussed in this chapter and discuss links to relevant Internet material. The samples include VBA code that I used to establish the numerical identity numbers of the Form View shortcut menu.




Real World Microsoft Access Database Protection and Security
Real World Microsoft Access Database Protection and Security
ISBN: 1590591267
EAN: 2147483647
Year: 2003
Pages: 176

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net