The Access Wizard Newsletter
Tips, Tricks, and Traps for Access Users and Developers.

From Events to Events – Up Your Power Level by Using One Event to Fire Another Event


The picture above shows a Rube Goldberg contraption: One event causes another event that causes another event … to a final outcome.

In this month’s Wizard, I will show you how you can use one event to trigger another event within Microsoft Access. Although you might think this is no big deal, once you start using this technique, you'll find that you will use it a lot, and it will also set you up for a much a more powerful technique, which I will review in the next Access Wizard.
 

Subroutines Associated with Buttons and Other Objects in Forms

When you make a form and place objects (command buttons, text boxes, option groups, etc.) on that form, there are events that fire every time you do almost anything on that form or object.

For the purposes of this Wizard, I will focus on the command button, but be aware that the technique is generic and can be widely used. I’ll start with a button called cmdButtonOne. In design view, if you right-click on the button, choose properties, then click on the event tab, you will see all the events associated with that button.

 

The event that is most commonly used with buttons is the On Click event. Any code that is placed in that event will fire every time the button is clicked.

As a test, let’s make a form and two command buttons, which we will call cmdButtonOne and cmdButtonTwo.

In design view, right click on the button, choose properties, click on the ellipsis, then choose Code Build next to the On click event as shown above.

You will end up with a code skeleton, which will fire every time a user clicks on the button. Add the code below, then save and close the form.
 
Private Sub cmdButtonOne_Click()
    MsgBox "This is Button One."
End Sub


When you click this button, you will get a message box that says “This is Button One.”

For second button, we will also create a click event. However now, we will redirect the second button to run the code in the first button. The good news is that it is pretty simple. Create the code skelton as you did for Button One, except this time, in the body of the code, add the line “cmdButtonOne_Click” as shown below.

Private Sub cmdButtonTwo_Click()
    cmdButtonOne_Click
End Sub


Save and close the form, then click on Button Two and you will see the message box from the first command button.
 

Why the Buttons Act as They Do

When you create an event for a button (or for any other object on a form), the default name consists of two parts: The first is the name of the button, followed by the underscore character, and then the name of the event.

For instance, if you have a button named cmdClientInfo and you create a click event, you will see that name of the event is

Private Sub cmdClientInfo _Click()

The first word on the subroutine Private means that the code is able to be seen any place within the form, but no place outside of the form. In essence, this means that you can cause that event to fire from any place in the form, not just from the click event of the button.

I use this technique frequently on combo boxes. Rather than using a combo box after update event to fire the code behind it, I use the companion event under the double-click command and translate it into the after update. With this approach, if the form has opened up to the last selection I made, then I can fire the after update event by simply double-clicking the button.
 

Conclusion and Coming Attractions

In this Wizard, I have parsed the components of an event within the form and shown that an event is able to be fired from any other part of that form. In the next Wizard, I will expand upon this within the form technique to make it even more powerful so that it can be seen and fired from any place within the application.
 

Tip of the Month: Show Hidden Objects


Prior to Access 2007, seeing hidden objects was a relatively simple procedure. You right-click on the object list and clicked on show hidden objects check box.

With the Ribbon and new object interface, it is a little bit more complicated but still available. The first thing you must have available to you is the navigation pane. If it's not visible, press the F11 key.

Once the navigation panel appears, click on the items below and you will be able to see hidden objects.

Options | Current Database | Navigation | Navigation Options | Show Hidden Objects



Quick Links:

Custom Software Home

Access Wizard Archives

Our Services

© Copyright Custom Software
All Rights Reserved | Westford, MA 01886