The Event Sequence for Controls

 < Day Day Up > 

The first set of events that you should be familiar with are events associated with controls (buttons, combo boxes, and so on). Let's look at the general topics of switching focus and changing data first, and then discuss selected events for individual controls.

NOTE

These lists of events are not comprehensive. There are many events that are of little practical use in most applications. For instance, although the text box control fires a mouse move event, most applications won't respond to this event. We've simplified the presentation so that you can get the big picture.


Focus Events

As the users of your application click and tab around the user interface of your forms, they trigger a flurry of events. You can use code associated with these events to highlight controls, update internal variables, or perform other tasks.

There are four key events here:

  • Enter Fired just before a control receives the focus from another control on the same form.

  • Exit Fired just before a control loses the focus to another control on the same form.

  • GotFocus Fired when a control gets the focus.

  • LostFocus Fired when a control loses the focus.

TIP

The focus refers to the indication that a particular control is the active control. Depending on the type of control, the focus might be indicated by a blinking cursor or a dotted outline.


To investigate the order in which these various events fire, you can add some code to the Clients form. Open the form in Design mode, open the form's module, and type in this code (or just use the version of the Clients form in the sample database for this chapter):

 

 Private Sub Client_Enter()   Debug.Print "Client Enter" End Sub Private Sub Client_Exit(Cancel As Integer)   Debug.Print "Client Exit" End Sub Private Sub Client_GotFocus()   Debug.Print "Client GotFocus" End Sub Private Sub Client_LostFocus()   Debug.Print "Client LostFocus" End Sub Private Sub Address_Enter()   Debug.Print "Address Enter" End Sub Private Sub Address_Exit(Cancel As Integer)   Debug.Print "Address Exit" End Sub Private Sub Address_GotFocus()   Debug.Print "Address GotFocus" End Sub Private Sub Address_LostFocus()   Debug.Print "Address LostFocus" End Sub 

The purpose of this code is to write messages to the Immediate window as events occur. Save and close the form. Arrange the windows on your screen so that you can see both the Access workspace and the VBA editor. Now open the form and you'll see that two of the events from this set are fired immediately, as shown in Figure 11.1.

Figure 11.1. Tracing the order of events.

graphics/11fig01.jpg


When you open the form for the first time, the focus has to go somewhere. In fact, it goes to the first control in the form's tab order. As you might expect from the earlier discussion, the Enter event is fired before the GotFocus event.

Now press the Tab key to move the focus from the Client text box to the Address text box. You'll see that this fires four more events:

 

 Client Exit Client LostFocus Address Enter Address GotFocus 

Click to activate another form in the TimeTrack database, such as the Switchboard form, and then click back on the Clients form. This fires two more events:

 

 Address LostFocus Address GotFocus 

The Exit and Enter events don't fire in this scenario, because they're only associated with movement to other controls on the same form. But the focus can only belong to one form at a time, so it moves away and then moves back when you switch to the switchboard form and back.

Finally, switch to another application entirely, such as an instance of Microsoft Excel or even Notepad, and then come back to Access. You might be surprised to find that this action does not trigger any further focus events. Access events are only concerned with things that happen in Access, not with what's going on in the rest of your computer.

NOTE

The Exit event is an example of an event that can be canceled. You learn more about this in the section "Canceling Events" later in this chapter.


     < Day Day Up > 


    Automating Microsoft Access with VBA
    Automating Microsoft Access with VBA
    ISBN: 0789732440
    EAN: 2147483647
    Year: 2003
    Pages: 186

    Similar book on Amazon

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