Team LiB
Previous Section Next Section

Attaching Code to a Custom Menu Item

We have yet to write any VBA code. However, once we start, we will probably want to have end users access the code via menus and buttons in the application. Up to this point, all we have done is create a few simple macros. We are going to put one of the macros we created on our little menu bar and then, in Chapter 13, use exactly the same technique to attach VBA code to a command bar.

For our example, let’s add a New Menu item as we just did in the previous steps. Rename this new menu item My Macros. The custom menu should look like this:

If you now select the All Macros category in the Customize dialog box, you should see a list of any macros you have created to this point. For the sake of simplicity, Figure 4-9 just shows the macFirst macro.

Click To expand
Figure 4-9: The All Macros category

You simply drag the macro’s icon and place it as a submenu item as you did with the previous menu item.

Let’s take a look at a menu feature we have not had a chance to look at yet by right-clicking on the new macro menu command we just put on the menu bar. A menu should come up, and when you select Properties, you should see the same dialog box shown in Figure 4-10.


Figure 4-10: Menu Control Properties dialog box

Here you can exercise some fine control over your menus as follows:

Note that if you now close the Customize dialog box and go to run this last macro, you will probably end up with the error message shown here.

Click To expand

This macro is associated with a form that we worked on earlier (its purpose is to take us to the first record in the form). Because the form is not open, the macro simply does not know what to do. In order for this macro to function properly, you would need to have the frmCustomer open so that it could go to the first record.

If you select OK in the first message box, the Action Failed message box, shown next, opens, allowing you to stop the macro.

Click To expand

Select Halt to shut down the macro.

In order for this macro to function properly, we would need to have the frmCustomer open so that it could go to the first record.


Team LiB
Previous Section Next Section