11.2. Using Events

 < Day Day Up > 

Events are some of the most useful things you can use to control a user interface. Events happen at the form level and at the individual control level. At the form and control levels, there are some key events I find very useful, which I outline here.

11.2.1. OnCurrent

The OnCurrent event runs each time the focus moves between records. There are several reasons to use this. First, based on values in a record, you might want to enable certain buttons and disable others. You can control this by setting the Enabled property of the command buttons based on If...Then statements or Select...Case statements. You might also want to perform some type of calculation or pull some data from another table based on what is in a current record. All of this can be done automatically using the OnCurrent event.

11.2.2. BeforeUpdate/AfterUpdate

The BeforeUpdate and AfterUpdate events are best used on text boxes, combo boxes, etc. The main difference between them is that BeforeUpdate fires prior to making the change to the field, and AfterUpdate fires after the change has been made to the field. Use BeforeUpdate when performing data validation. If the data is not what you expected, take actions to ensure proper data. Depending on what you are trying to do, you might want to test both to make sure that you use the correct one. If you use these events on a control connected to data (and the name of the field is different from the name of the control), when the BeforeUpdate event fires, the text box holds the current value shown on the form, and the field name holds the old value. When the AfterUpdate event fires, they will have the same value.

11.2.3. OnChange

The OnChange event fires when a control's value changes. Since it fires as you type each character in a control, it is often more appropriate to use the AfterUpdate event. However, for situations when you perform data validation, using the OnChange event can be the right choice if you need to know when a value changes.

11.2.4. OnDirty

The OnDirty event fires any time the contents of a form or text box change (depending on the object where you have the event set). It is useful if you want to set a global variable when a change happens. Essentially, the OnDirty event lets you know that something happened that will change the data in your application. Since you will find out prior to a change happening, you can write code to handle changes, which is useful if you need to perform time consuming data validation and only want to do it when something has changed.

11.2.5. OnEnter/OnExit

The OnEnter and OnExit events run when you enter or exit a control. They redirect the flow of the application but give you more control than you get by just setting the tab order of the controls on the form.

11.2.6. OnClick/OnDoubleClick

These events run when a control is clicked or double-clicked. Although I rarely use the OnClick event except for command buttons, I often use the OnDoubleClick event to allow users to add records on the fly to a combo box. Assume you have a combo box that displays values from a table and a form called frm_Table that allows you to add records to the table. Put the code shown in Example 11-1 in the OnDoubleClick event of the combo box.

Example 11-1. OnDoubleClick event example
 Private Sub Combo2_DblClick(Cancel As Integer) DoCmd.OpenForm "frm_Table" Me.Text0.SetFocus End Sub 

Now, if a user needs to add records to the combo box, she can just double-click on the combo box, and the double-click event opens the form to add records and move the focus to a different text box (so that the requery event, shown next, works). You know that the user has completed entering information in the form when the focus returns to the original form. Use the code in Example 11-2 for the form's GotFocus event.

Example 11-2. GotFocus example
 Private Sub Form_GotFocus(  ) Me.Combo2.Requery End Sub 

While you might be tempted to put the Requery method in the OnDoubleClick event, Access opens the form and requeries immediately, which results in the data not being updated for what was just entered. I have also worked with applications that have requery or refresh buttons beside combo and list boxes, allowing the user to control when they are refreshed. I think that using the GotFocus event is a more elegant solution than making the user press extra buttons to perform a refresh.

In addition to using buttons to perform a refresh of data, I have also worked with applications that use buttons to add values to combo boxes. In my opinion, using the OnDoubleClick event to add records is a better solution and leads to less clutter on the form.

11.2.7. OnOpen

The OnOpen event fires when the form is first opened. Because the user may have maximized other forms, if I have a form that I do not want shown maximized, I use the OnOpen event to execute the DoCmd.Restore command. This is the equivalent of pressing the button in the command bar between the minimize and close buttons when a form is maximized. You can also put in code to perform other actions that you want to take place when a form is opened.

11.2.8. OnTimer

The OnTimer event works along with the Timer Interval property of the form that is set in milliseconds and is very useful for a number of functions. For example, you can have a database application that processes files in a directory. You can have a form open to check for new files in a directory every 15 minutes by setting the Timer Interval property to 900,000 (15 minutes x 60 seconds x 1000 to convert seconds to milliseconds).

     < Day Day Up > 


    Integrating Excel and Access
    Integrating Excel and Access
    ISBN: 0596009739
    EAN: 2147483647
    Year: 2005
    Pages: 132

    Similar book on Amazon

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