|
Actions for EventsFrom 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:
Creating ActionsTo 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. Here you select the event you want to write action for. To the right you will see an ellipsis, click on this ellipsis. The Choose Builder dialog box appears. 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. 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 ActionsTo 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 run button. If you have an error and want to stop the program from running click on the quit button. Using Code to change PropertiesIn 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 ExampleThe 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.
SetFocus MethodAnother 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. ExampleThe 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. 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 CommandThis 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) ExampleThe 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. |