Ms Access 2010 tutorial: Events


Events

IV. Events

Events are what the controls have to perform when there are actions on them. In Access 2010, there are a lot of events, but in this section we introduce you some events that are frequently used . Those events are:

IV.1. On Load

On Load event occurs when a form starts. For example, when the form opens, you want the form to display the words "Welcome Access 2010". To do this task, do the following:

- Open the form in Design View

- Press F4 if the Property Sheet is not displayed

- Click Event tab

- Click Access three dot in front of On Load event

Access on load 

Access displays the Choose Builder dialog box as the figure below:

Access choose builder dialog box 

- Click Code Builder --> Ok

Access opens the VBA window with Form_Load event procedure as the figure:

Access vba window 

- Type the VBA code between Private Sub Form_Load() and End Sub as the following:

Option Compare Database
___________________________
Private Sub Form_Load()
MsgBox "Welcom Access 2010!"
End Sub

Access on load message

IV.2. On Close

On Close event happens when a form is closed. For instance, when the form is closed, you want the form displaying a message that "Thank you for using this form!". To do so, do the following:

- Open the form in Design View

- Press F4 if the Property Sheet is not displayed

- Click Event tab

- Click Access three dot in front of On Close event

Access displays the Choose Builder dialog box.

- Click Code Builder --> Ok

Access opens the VBA window with Form_Close event procedure.

- Type the VBA code between Private Sub Form_Close() and End Sub as the following:

Option Compare Database
_______________________________
Private Sub Form_Close()
MsgBox "Thank you for using this form!"
End Sub

Access on close message

IV.3. After Update

After Update event occurs after the updated data in a control or record is updated. For example, after you updated data in any controls on the frmStudent, you want the form to display a message to inform that "Data has been updated". To do this task, do the following:

- Open the form in Design View

- Press F4 if the Property Sheet is not displayed

- Click Event tab

- Click Access three dot in front of After Update event

Access displays the Choose Builder dialog box.

- Click Code Builder --> Ok

Access opens the VBA window with Form_AfterUpdate event procedure.

- Type the VBA code between Private Sub Form_AfterUpdate() and End Sub as the following:

Option Compare Database
_______________________________
Private Sub Form_AfterUpdate()
MsgBox "The data has been updated.", vbInformation
End Sub

Access after update message

IV.4. Before Update

This event happens before the data in a control or record is updated.

IV.5. On Delete

The event occurs when the data on a form is deleted. The action of this event performs before the data is really deleted. For example, once you delete a record in list box, the form displays a message that "Your data has been updated".

IV.6. On Timer

On Timer event happens when computer's timer runs and the Timerinterval property is set more than zero. For Example, once the frmBookInfo actives the title of the form changes from one color to another color (15 different colors). To solve the example, do the following:

- Open the frmBookInfo in Design View

- Press F4 if the Property Sheet is not displayed

- Click Event tab

- Click Access three dot in front of On Timer event

Access displays the Choose Builder dialog box.

- Click Code Builder --> Ok

Access opens the VBA window with Form_Timer event procedure.

- Type the VBA code between Private Sub Form_Timer() and End Sub as the following:

Option Compare Database
_______________________________
Private Sub Form_Timer()
lblTitle.ForeColor=QBColor(rnd*15)
End Sub

To make the Timer event can run, you need to define TimerInterval's value is more than zero in Form_Load event procedure.

Option Compare Database
_______________________________
Private Sub Form_Load()
TimerInterval = 500
End Sub

The more you set TimerInterval property with a big value, the more timer's speed is lower and lower and vice versa. TimerInterval's measure is in milliseconds and its value (interval) can be between 0 and 2,147,483,647.

IV.7. On Click

ction that occurs when an object is click (left click). For instance, once you click the OpenForm button on the Form1 the frmBook displays. For this example, do the following:

- If you do not have the form1, create it  by using Form Design

- Drag and drop a Button on the form

- Click the Button, and then set the Caption and Name property of the Button in the Property Sheet as the following:

If the Property Sheet is not displayed, press F4 to open it.

    + Caption: OpenForm

    + Name: CmdOpen 

- Click Event tab

-Click Access three dot in front of On Click event

Access displays the Choose Builder dialog box.

- Click Code Builder --> Ok

Access opens the VBA window with CmdOpen_Click event procedure.

- Type the VBA code between Private Sub CmdOpen_Click() and End Sub as the following:

Option Compare Database
_______________________________
Private Sub CmdOpen_Click()
DoCmd.OpenForm "frmBook", acNormal
End Sub  

Note: The Caption property of the Button is in the Format tab of the Property Sheet while the Name property is in the Other tab.

IV.8. On Lost Focus

An action that happens when a cursor leaves from an object. For example, when a user missed to enter data in the BookID Text Box of frmBook, the form displays a message that "The primary key field cannot be blank". To do this task, do the following:

- Open the form in Design View

- Click txtBookID text box

- Press F4 if the Property Sheet is not displayed

- Click Event tab

- Click Access three dot in front of On Lost Focus event

Access displays the Choose Builder dialog box.

- Click Code Builder --> Ok

Access opens the VBA window with txtBooKID_LostFocus event procedure.

- Type the VBA code between Private Sub txtBooKID_LostFocus() and End Sub as the following:

Option Compare Database
_______________________________
Private Sub txtBooKID_LostFocus()
If Trim(txtBooKID.Text) = "" Then
MsgBox "Primary key field cannot be blank.", vbInformation
End If
End Sub

IV.9. On Got Focus

An action that happens when a cursor focuses on an object.

IV.10. On Dbl Click

An action that occurs when you press and release the left mouse button twice. Form example, once you double-click a value in the List Box this value displays in Text Box as the figure below:

Access on double click event  

If you do not has the Form2 as the figure above, you need to create it. The form2 constructs with a List Box and Text Box. The name of the Text Box and List Box as the following:

Control           Name
Text0              txtData
List Box          LstData

- Open the form in Design View

- Click  lstData lsit box

- Press F4 if the Property Sheet is not displayed

- Click Event tab

- Click Access three dot in front of On Dbl Click event

Access displays the Choose Builder dialog box.

- Click Code Builder --> Ok

Access opens the VBA window with LstData_DblClick event procedure.

- Type the VBA code between Private Sub LstData_DblClick(Cancel As Integer) and End Sub as the following:

Option Compare Database
_______________________________
Private Sub LstData_DblClick(Cancel As Integer)
    txtData.Value = lstData.ItemData(lstData.ListIndex)

Or
     txtData.Value = lstData.Column(0, lstData.ListIndex)

End Sub

The List Box on the Form2 contains values when the form loads. In default, the List Box does not contain value. To add values to List Box,   you need to apply the following VBA code on Form_Load event:

Option Compare Database
_______________________________
Private Sub Form_Load()   lstData.RowSource = ""
  lstData.RowSourceType = "Value List"
  With lstData
   .AddItem 12
   .AddItem 13
   .AddItem 45
   .AddItem "Hello"
  End With
End Sub End Sub

IV.11. On Change

An action that happens when data in the Text Box or Combo Box are changed. For example, you have a form as the figure below:

Acces on change event 

If you type number one in Number Text Box,  it displays the word "One" in Description Text Box;  if you type number two in Number Text Box, it displays the word "two" in Description Text Box.  To do this task, do the following:

- Create a form as Form 9 and set the name of the two Text Boxes as the following:

Control           Name
Text0              txtNumber
Text1              txtDes

- Open the form in Design View

- Click txtNumber Text Box

- Press F4 if the Property Sheet is not displayed

- Click Event tab

- Click Access three dot in front of On Change event

Access displays the Choose Builder dialog box.

- Click Code Builder --> Ok

Access opens the VBA window with txtNumber_Change event procedure.

- Type the VBA code between Private Sub txtNumber_Change() and End Sub as the following:

Option Compare Database
_______________________________
Private Sub txtNumber_Change()
   txtDes.SetFocus
   If txtNumber.Value = 1 Then
      txtDes.Value = "One"
   ElseIf txtNumber.Value = 2 Then
      txtDes.Value = "Two"
   Else
      txtDes.Value = "Not mention"
   End If
   txtNumber.SetFocus
End Sub

IV.12. On Key Press

An action that happens when the keyboard is pressed. For example, you want to allow only number to enter in the Number Text Box. To do so, do the following:

- Open the form in Design View

- Click txtNumber Text Box

- Press F4 if the Property Sheet is not displayed

- Click Event tab

- Click Access three dot in front of On Key Press event

Access displays the Choose Builder dialog box.

- Click Code Builder --> Ok

Access opens the VBA window with txtNumber_KeyPress event procedure.

- Type the VBA code between Private Sub txtNumber_KeyPress(KeyAscii As Integer) and End Sub as the following:

Option Compare Database
_______________________________
Private Sub txtNumber_KeyPress(KeyAscii As Integer)
   If KeyAscii >= 48 And KeyAscii <= 57 Then
      Exit Sub
   Else
      KeyAscii = 0
   End If

End Sub


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.