At this point, you have developed a well-functioned application and you’re ready to turn it over to the end users… well, not quite yet. When you open the Access file, there are lots of neat things like tables and queries, macros, and VBA – all the sorts of items that we developers like to work with. Most end users are not going to want to see this. More important, if you have to support it, you don’t want them seeing it either, much less have the ability to change any of it.
In this chapter, I’ll show you some ways to package the application with the end user in mind. The first step is to provide a way for them to navigate to the forms that they will be using. I’ll demonstrate two ways to accomplish this using a custom navigation form and a custom ribbon. With that in place, you’ll then need to remove the standard navigation and lock down the application to prevent unwanted alterations.
Form Navigation
When the application is first loaded, you will need some sort of “welcome” form that will present to the user their choices of things they can do such as check out a customer, look for an item, and so on. Access 2010 provides a really nice facility for creating a navigation form by simply dragging the forms and reports to the appropriate navigation structure. However, the complex forms you have developed so far will not work with this technique. Essentially, the navigation form becomes the main form and all your other forms are added as subforms. As I explained in Chapter 8, there are limitations with subforms. For example, you cannot use a Split Form as a subform.
Note I will demonstrate the built-in Navigation Form template in Chapter 15. You will use this to organize the web forms.
So you’ll need to create your own custom navigation form. Fortunately, this is simple to do – almost surprisingly so. The hardest part is usually deciding what options should be allowed and how to best organize them. Although there is no right way to organize these, I suggest the following structure:
• Operations (tasks that you do all day long)
• Checkout a customer
• Set up a new customer or view/modify an existing one
• Lookup an item
CHAPTER 10 ENHANCING THE USER EXPERIENCE
• Administration (updating configurable objects)
• Items
• Categories
• Media types
• Maintenance (routine tasks for ongoing support and maintenance)
• Calculate late fees
• Cancel old requests
Creating the Menu Form
You’ll create a blank form and place command buttons on it. You can take advantage of the control wizards that will set up a macro to open the associated form when the user clicks a command button.
You will use a TabControl to organize the buttons into the three top level items (Operations, Administration, and Maintenance).
1. Click the Blank Form button in the Create tab of the ribbon.
2. In the Design tab of the ribbon, click the Tab Control button, and then draw a rectangle on the form. This will create a TabControl with two pages. Right-click this control and click the Insert Page link. Select each page and set the Name property of each to Operations, Administration, and Maintenance. The form should look like Figure 10-1.
Figure 10-1. The initial Menu form layout
3. In the Property Sheet, select the Form object and set the Record Selectors and Navigation Buttons properties to No. These are not applicable for this form, because it does not access a table.
257
Tip Make sure that the control wizards are turned on. To check, click the dropdown icon near the bottom-right corner of the Controls section of the ribbon. This will show all of the available controls. The icon next to the Use Control Wizards icon should be highlighted as shown in Figure 10-2. If not, click the Use Control Wizards link to turn them on.
Figure 10-2. Checking the status of the control wizards
4. Click the Button button and then click the Operations page. This will launch the Command Button Wizard. In the first dialog box, select the OpenForm action as shown in Figure 10-3.
Figure 10-3. Selecting the OpenForm action
5. In the second dialog box, select the CheckOut form, as shown in Figure 10-4.
CHAPTER 10 ENHANCING THE USER EXPERIENCE
Figure 10-4. Selecting the CheckOut form
6. The third dialog box, shown in Figure 10-5, provides an option to filter the record when opening the form. This doesn’t apply in this scenario, so select the second option, which is to show all records.
Figure 10-5. Selecting the option to show all records
7. The fourth dialog box is used to specify the text or picture that should be displayed on the button. Select the Text option and enter Check Out, as shown in Figure 10-6.
259 Figure 10-6. Specifying the button caption
8. In the final dialog box, enter the name CheckOut, as shown in Figure 10-7.
Figure 10-7. Specifying the name of the CheckOut button
9. You should now have a button on the Operations page that will open the CheckOut form. Repeat this process to add buttons that will open the CustomerAdmin and Item forms. The page design should look like Figure 10-8.
CHAPTER 10 ENHANCING THE USER EXPERIENCE
Figure 10-8. The Operations page layout
10. Using the same approach, add buttons to the Administration page to open the following forms:
• Item
• Category
• Media
On the Maintenance page, you’ll create buttons that will call a macro. There are two macros included in the Navigation pane: CalculateLateFees and CancelOldRequests. You created these in Chapters 3 and 4 to demonstrate how to call a data macro and an action query. Now you’ll provide buttons in the Menu form to allow the user to call these.
11. Add a command button to the Maintenance page. In the Command Button Wizard, select the RunMacro action, which you’ll find in the Miscellaneous category, as shown in Figure 10-9.
Figure 10-9. Selecting the RunMacro action
261 10-10.
Figure 10-10. Selecting the CalculateLateFees maco
13. Fill out the rest of the dialog boxes as you did for the other command buttons.
14. Repeat this process to create another button that will call the CancelOldRequests macro.
15. The Maintenance page should look like Figure 10-11.
Figure 10-11. The completed Maintenance page
Save the form and enter the name Menu when prompted. Switch to the Form view. Try clicking the buttons and verify that the appropriate forms are loaded and that they work as expected.
CHAPTER 10 ENHANCING THE USER EXPERIENCE
Auto-Loading the Menu Form
The Menu form is a handy way for the user to start any of the forms designed for them to access. Now you’ll configure Access to load this form automatically when the application is started.
1. Go to the File tab of the ribbon, which displays the Backstage View.
2. Click the Options button to display the Access Options dialog box.
3. Select the Current Database tab and select the Menu form in the Display Form combo box as shown in Figure 10-12. This tells Access to load the Menu form whenever this file is opened.
Figure 10-12. Selecting the Menu form to be auto-loaded
When you save the changes, you’ll get the pop-up dialog shown in Figure 10-13, which lets you know that this change will take affect the next time the file is loaded.
Figure 10-13. Reminder to close and reopen the database
Close the Access application and re-open the Library.accdb file. You should see the Menu form start automatically.
Ribbon Navigation
Another approach to providing navigation to your forms is to customize the ribbon. You can add a tab to the existing ribbon or create your own custom ribbon, with an XML file and a little bit of VBA code. I’ll first demonstrate how to add a new tab to the ribbon, and then I will show you how to build your own data-driven menu using a custom ribbon.
263
Implementing a Sample Ribbon Tab
A sample XML file that creates a new ribbon tab is shown in Listing 10-1.
Listing 10-1. Sample Script to Create a Custom Ribbon Tab
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon startFromScratch="false">
<tabs>
<tab id="demo1" label="Demo Tab">
<group id="group1" label="Sample">
<button id="button1" size="large" label="Sample1"
screentip="Sample 1" supertip="This is a sample button"
getImage="GetImage" onAction="OnMenuAction" />
<button id="button2" size="normal" label="Sample2"
screentip="Sample 2" supertip="This is a sample button"
getImage="GetImage" onAction="OnMenuAction" />
<button id="button3" size="normal" label="Sample3"
screentip="Sample 3" supertip="This is a sample button"
getImage="GetImage" onAction="OnMenuAction" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
The ribbon element has a single attribute called startFromScratch. If this is set to false, as it is here, the defined tabs are added to the existing ribbon. Use this option if you want to add a custom tab but leave the standard tabs in place. If you set this to true, all the existing tabs will be removed and the ribbon will only contain whatever is defined in this XML script.
The ribbon element contains a tabs collection, which has a single tab labeled “Demo Tab.” Each tab then defines one or more group elements. This script has a single group labeled “Sample,” which contains three button elements.
Each button element specifies the following attributes:
• id: A unique identifier for this button
• size: The size of the graphic for this button (either large or normal)
• label: The text that is displayed with the button
• screentip: Heading for the hover text
• supertip: The hover text that is displayed when the mouse is over this control
• getImage: The name of a callback function that supplies the image for this button
• onAction: The name of a callback function that is called when the button is clicked
• tag: Not shown here, but can be used for providing details to the callback functions
CHAPTER 10 ENHANCING THE USER EXPERIENCE
To add this custom tab to your ribbon, you’ll need to implement the two callback functions, GetImage and OnMenuAction. Then you will also need to install this ribbon in the database.
At the bottom of the navigation pane, you should see the Main module. Double-click this to display the VBA editor. Add the code shown in Listing 10-2 to the Main code file.
Listing 10-2. The Initial Implementation of the Ribbon Callback Functions Public Sub GetImage(ByVal control As Office.IRibbonControl, ByRef image)
image = "HappyFace"
End Sub
Public Sub OnMenuAction(ByVal control As Office.IRibbonControl)
MsgBox "You've clicked the button " & control.ID & " on the Ribbon"
End Sub
The GetImage callback returns the HappyFace icon, which I’ll explain later. The OnMenuAction callback simply displays the ID of the control that was clicked.
Note You will need to add a reference to the Office Object Library. In the VBA Editor, click the Tools menu and then the References link. In the References dialog box, add the Microsoft Office 14.0 Object Library, as shown in Figure 10-14.
Figure 10-14. Adding the object library reference
265 chapter, I’ll show you a second way. In the first approach, you’ll create a table named USysRibbons and copy the XML into this table. When a database is opened, Access looks for the USysRibbons table and will automatically install any ribbons that are defined there.
1. From the Create tab of the ribbon, click the Table Design button.
2. Add the following fields:
• RibbonID: AutoNumber (set this as the primary key)
• RibbonName: Text
• RibbonXML: Memo
3. Save the table and enter the name USysRibbons when prompted.
4. Open the table in the Datasheet View. Enter Demo in the RibbonName field.
Enter the XML shown in Listing 10-1 in the RibbonXML field and save the record.
The table should look like Figure 10-15.
Figure 10-15. The contents of the USysRibbons table
Close the Access database and then re-open it. Access should have loaded your custom ribbon, but now you need to tell Access to use it.
5. Click the File tab to display the Backstage View.
6. Click the Options button and then select the Current Database tab.
7. About halfway down, in the “Ribbon and Toolbar Options” section, select the Demo ribbon, as shown in Figure 10-16.
CHAPTER 10 ENHANCING THE USER EXPERIENCE
Figure 10-16. Selecting the Demo ribbon
Close the dialog and you will be reminded that you’ll need to close the database and re-open it.
Do that now and when the database is reloaded you should see a Demo tab like the one shown in Figure 10-17.
Figure 10-17. The custom Demo ribbon tab
Try clicking the buttons in the custom tab. You should see a pop-up window that tells you the name of the button that was clicked.
Tip As you probably know, XML is not very forgiving. The slightest syntax error will prevent the file from loading. If Access encounters an error in processing the XML it will simply ignore your custom ribbon. You won’t see any errors, but you won’t have a custom tab. This can be frustrating when trying to find where the problem is.
While developing and testing custom ribbons, you can enable these error messages. Click the Options button on the Backstage View. In the Client Settings tab, scroll down to the General section. Select the “Show add-in user interface errors” check box, as shown in Figure 10-18.
267 Figure 10-18. Enabling UI error messages
Displaying the System Objects
You may have noticed that, when you re-opened the database, the USysRibbons table was gone. It’s not really gone, but merely hidden from the Navigation pane. Access has several system tables that it uses and these are normally hidden, that is, not included in the Navigation pane.
In addition, any table that you create that starts with “USys” is considered a user-system table and is also hidden. So when you first created the USysRibbons table it was available to you. However, when the database was re-opened, this was no longer visible.
To show both system and user-system tables, click the Options button in the Backstage View. Select the Current Database tab and click the Navigation Options button that is shown in Figure 10-19.
CHAPTER 10 ENHANCING THE USER EXPERIENCE
Figure 10-19. Displaying the Navigation Options
In the Navigation Options dialog, shown in Figure 10-20, you can control the types of objects that are included and how they are organized.
Figure 10-20. Enabling the system objects
Select the Show System Objects check box and click the OK button. Then click the OK button to close the Access Option dialog box. You’ll see a pop-up saying the changes will not be applied until the database is reloaded. In this case, that’s not true. The system tables are added to the Navigation pane immediately.
You should see the USysRibbons table in the Navigation pane and you can view and update its contents. You should also notice the USysApplicationsLog that you used in Chapter 3 to diagnose data macro errors.
Building a Custom Ribbon
Now that you have the basic concepts for building a custom ribbon, you’ll develop a data-driven menu feature. This will allow you to design the menu options using a form and then generate the appropriate XML based on the data. As you add new options, the ribbon will automatically adjust.
As I explained earlier, a ribbon uses the following three-level hierarchy for organizing the controls:
269
• Tabs
• Groups
• Commands
To accommodate this in your design, you’ll have a table for each of these levels. You’ll then create a form that will allow you to define each of these elements. Once the implementation is done and the data is entered, you’ll write VBA code to extract the data and generate the corresponding XML script.
Designing the Tables
You’ll start by creating three tables to define the three levels of the ribbon hierarchy. These tables are not really part of the Library data but are internal tables used by the application. To differentiate these, the name will have a “mnu” prefix. The instructions below are abbreviated; you can refer to Chapter 2 if you need more help in designing tables.
1. Create a mnuTab table using the following fields:
• TabID: AutoNumber, primary key
• Label: Text (100)
• Sequence: Number (Integer)
2. Save the table and close it. Then create the mnuGroup table including the following fields:
• GroupID: AutoNumber, primary key
• TabID: Lookup (use the mnuTab table)
• Label: Text (100)
• Description: Text (255)
• Sequence: Number (Integer)
Note When setting up the Lookup column, select both the TabID and Label fields. Use the defaults values for the remaining dialog boxes.
3. Save the table and close it. Finally, create the mnuCommand table as follows:
• CommandID: AutoNumber, primary key
• GroupID: Lookup (use the mnuGroup table)
• Label: Text (100)
• Graphic: Text (100)
CHAPTER 10 ENHANCING THE USER EXPERIENCE
• GraphicSize: Lookup (enter the allowed choices as large and normal)
• TargetType: Lookup (enter the allowed choices as Form, Report, and Macro)
• Target: Text (100)
• ScreenTip: Text (100)
• SuperTip: Text (255)
• Sequence: Number (Integer)
Note When setting up the GroupID lookup column, select both the GroupID and Label fields. Use the remaining default values.
4. Open the mnuTab table in the Datasheet view. Add a single record with the Label field Library and Sequence =1.
Creating the Menu Forms
You’ll start by generating a form for the mnuCommand table. Then you’ll use this as a subform on the mnuGroup form. Because the number of tabs is small, you’ll just enter these in the mnuTab table directly.
1. Select the mnuCommand table in the Navigation pane and click the Form button in the Create tab of the ribbon.
2. Delete the CommandID and GroupID controls and their associated labels.
3. Set the control padding to None.
4. Set the Text Align property for all of the labels to Right.
5. Remove the Form Header.
6. Change the Default View to Continuous Forms.
7. Re-arrange the controls to look like Figure 10-21.
Figure 10-21. The design of the mnuCommand form
8. Save the form and enter the name mnuCommand when prompted. Now you’ll create the mnuGroup form.
271 Create tab of the ribbon.
10. This will create Datasheet view to display the associated records of the mnuCommand table. Delete this and add a Subform control in its place. The Subform Wizard will find multiple fields in both table with the same name and will prompt you to select the correct field to use for linking the forms. Select the GroupID field, as shown in Figure 10-22.
Figure 10-22. Selecting the GroupID field for linking the forms 11. Arrange the controls as shown in Figure 10-23.
CHAPTER 10 ENHANCING THE USER EXPERIENCE
Figure 10-23. The mnuGroup form layout
Save the form and enter the name mnuGroup when prompted.
Populating the Menu Tables
Now you’ll use the mnuGroup form to define the controls that will be placed on the ribbon. You’ll follow the same basic structure that you used for the Menu form that you created earlier in the chapter. There will be three groups (Operations, Administration, and Maintenance). The first group will look like Figure 10-24.