Events


Most programming environments are event-driven systems. You roll the mouse pointer over a button and click the left mouse button. The clicking of the mouse button is an event. Another block of programming code listens for the event. We call this block the event listener. The event listener then sends a message to the block of code that is supposed to work when the event happens. This block is called the event handler.

The mouse click may not be the only event. As you will see shortly, things like opening a form, closing a form, selecting an item from a list, and so on can all be events.

Microsoft Windows already has the code to handle routine events built right into it. Because VBA and Access are so closely tied to the Windows environment, a lot of routine functions will be handled without your writing a drop of programming code. All you need to do is tell Access what action you want to occur.

Once you assign the action, Access writes an event procedure. This is a small block of VBA code that is written for you. Let’s give it a try.

Create a Macro

Open the Forms category in the Database window for TheCornerBookstore. Open the frmCustomer form. It should look like Figure 3-5.

click to expand
Figure 3-5: Customer entry form

Try clicking the buttons. You will notice that they don’t do anything right now. That is because we have not selected an event listener or an event handler. Let’s take a look at the steps to do this.

Right-click on the First button and select Properties. Once you are in the dialog window, select Events. You should see something like Figure 3-6.


Figure 3-6: Events for the First button

Notice that there are 12 potential events for just one button. They also all begin with the word “On.” The most common is the On Click item. Click inside that field and select the button, located on the right, with the ellipses inside it. When you do, you will be asked to choose your builder, as shown in Figure 3-7.


Figure 3-7: The Choose Builder dialog box

The Expression Builder allows you to set up expressions that perform mathematical operations, compare values in other fields, and employ some limited decision-making capability.

The Code Builder takes you into the VBA Editor. We will talk more about that beginning in Chapter 5.

Select the Macro Builder. You will see something like this, prompting you to give the macro a name:

After clicking OK you are brought to the dialog box shown in Figure 3-8. (Note that, for clarity, Figure 3-8 shows the dialog box with the fields filled in.) Notice that this window is divided into two distinct areas, the Action area at the top and the Action Arguments area at the bottom.

click to expand
Figure 3-8: The Macro Builder dialog box

In this example, GoToRecord is selected as the action. Then, for the argument, we selected First as the record we want to go to.

All you need to do now is click on the save icon and close this dialog box. That’s it! Your macro is finished.

As a little exercise, if you are sitting at a computer, why don’t you follow the same procedure for the other command buttons? The only thing that will change will be the argument or record you will be choosing. The arguments will be Last, Previous, Next, and New. Not one drop of code was written. You can test it now.

There is one thing worth noting here. If you once again select Properties, and go to the All tab, you will notice that the button is named cmdFirst, as shown in Figure 3-9.


Figure 3-9: The name field in the properties box

That was named for you in this downloaded database. However, if you were doing the developing, you would need to name it yourself. If you are going to do VBA programming, you must give each object in your application a unique name. If you don’t, VBA won’t know what you are talking about. For instance, let’s say you wanted to attach an action to a button. VBA would ask you what button you are talking about.

Macros are very limited in what they can do for you. You can’t incorporate them directly into a standard VBA programming environment, but you can convert them into VBA code.

Converting Macros into VBA Code

Let’s convert our button macros into VBA code. Start by opening the frmCustomer form in Design View and selecting Tools | Macro | Convert Form’s Macros to Visual Basic. You will be prompted as to whether you want to add error-handling capabilities and comments:

click to expand

We will be discussing these options as we get more into VBA code. For the time being, just select Convert. You should be prompted when the conversion is finished. Just click OK, and then select Tools | Macro | VBA Editor. You should see a window similar to the one in Figure 3-10.

click to expand
Figure 3-10: VBA Editor with the converted macros

You should see the converted code. Without getting into detail yet, you will see a VBA sub procedure, which begins with:

Private Sub cmdFirst_Click()

This means that it is a VBA sub procedure, which will handle a Click event for the cmdFirst button.

Within this sub procedure, you should see the following line of code:

DoCmd.GoToRecord , "", acNext

This is essentially the code produced by the macro that tells VBA to instruct the form to go to the first record. (Remember, VBA cannot access records directly.)

While we generated some code, we did not do any coding on our own. Again, you can see the power of Access as a development tool.




Access VBA Programming
Microsoft Access VBA Programming for the Absolute Beginner
ISBN: 1598633937
EAN: 2147483647
Year: 2006
Pages: 214
Authors: Michael Vine

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