Lesson 3
Home Up Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5

ASCII table

Assignment 3

Actions for Events

From the first lesson, we learned that Windows, the operating system, detects when the user interacts with an input device. Any movement of the mouse, click of a button, key is an Event. Your job as a programmer is to write Actions that you want the computer to execute when certain events are detected by Windows.

Therefore the three tasks for creating programs is to:

  1. Create the interface, that is, to generate, position and size the objects
  2. Set properties, that is, set relevant properties for the objects
  3. Write the code that executes when the events occur.

Creating Actions

To enter actions for an object, select the object that you want to write the actions for. The first thing that you must do is Name the control that is going to contain the Action. Then click on the Events tab in the property window.

wpe8.jpg (13919 bytes)

Here you select the event you want to write action for. To the right you will see an ellipsis, click on this ellipsis.

wpe3.jpg (2380 bytes)

The Choose Builder dialog box appears.

wpe6.jpg (8359 bytes)

Select the Code Builder option and click on the OK button. This will open the Form Module, this is the object where you will create the code to produce the action you desire.

wpe9.jpg (12212 bytes)

The Code that begins with Private Sub and ends with End Sub, is called the Event handler. It is between these two lines that make the event procedure.

Running Actions

To run a form, select it from the available forms with the Open option.

To run a form from the Code module editor click on the wpe5.jpg (773 bytes) run button. If you have an error and want to stop the program from running click on the wpe6.jpg (689 bytes) quit button.

Using Code to change Properties

In earlier lessons you changed properties while you were designing the program. These are called Design Time property settings. Today we will study how to change properties while the program is running. These are called Run-Time property settings.

Statement to change properties is:

objectname.property = setting value

Example

The following event procedures handle three different events. Each one changes the caption of a button named cmdChange.

Event MouseDown occurs when the left mouse button is held down on the object. The action for that event changes the current Caption of the command button to the string Ouch.

Event MouseUp occurs when the left mouse button is released. The action for that event changes the current Caption of the command button to the string Thank you!.

Event MouseMove occurs when the mouse is moved over the object. The action for that event changes the current Caption of the command button to the string Hey where are you going?. However notice that the object is Detail, so this event only occurs when the mouse moves over the detail object.

wpe2.jpg (33188 bytes)

SetFocus Method

Another important command that you will use a great deal in VBA is the SetFocus method. (Remember a method is pre-coded procedures that do particular tasks.) The SetFocus method makes the object that runs the method, the active object.

Example

The code below gives the focus to the textbox txtName. When a textbox receives focus, the cursor appears in the box. If the textbox has text in it, then the text is highlighted.

wpe2.jpg (2811 bytes)

When objects receive focus, they react in different ways. If a command button receives focus, then a dashed rectangle surrounds the caption and if the Enter key is pressed, it activates the OnClick event for that button.

***Note that, labels can not receive focus.

DoCmd.Close Command

This command will close a form, reports, queries and other objects.

The syntax for this command is:

DoCmd.Close ObjectType,ObjectName,actiontype

ObjectType must be: acDefault (default), acForm, acMacro, acModule, acQuery, acReport, acTable

ObjectName is the name given to the object.

ActionType must be: acSaveNo (this closes the object without saving changes), acSavePrompt (this closes the object after displaying a dialog box asking if you want to save changes or not), acSaveYes (this closes the object and saves changes)

Example

The following code closes the form called Example Lesson 3 and if changes are made to it, a prompt will appear asking to Save or Discard changes.

wpe3.jpg (5207 bytes)

Assignment