Skip to Content
Course content

5.2. Event-Driven Programming in Access: Control Events

Control events in Microsoft Access form the foundation of event-driven programming, where user interactions with various form controls (like buttons, textboxes, combo boxes, etc.) trigger specific actions or code. These events allow for dynamic, responsive behavior in the application, offering a more interactive user experience.

1. What Are Control Events?

Control events occur when users interact with the individual controls (e.g., buttons, textboxes, combo boxes) on a form. Each control has its own set of events that can trigger actions, such as validating input, modifying other controls, or performing calculations. These events allow developers to add functionality and responsiveness to forms.

2. Types of Control Events

Here are some of the most commonly used control events in Access:

a. Button Click Event

  • Trigger: Occurs when the user clicks on a button control.
  • Use: Typically used for actions like saving data, navigating to other forms, running queries, or opening reports.
Private Sub btnSave_Click()
    ' Save form data and show confirmation message
    DoCmd.Save
    MsgBox "Data saved successfully!"
End Sub

b. TextBox After Update Event

  • Trigger: Occurs after a user has entered or edited a value in a textbox and then moves to another control.
  • Use: Useful for performing actions or calculations based on the value entered by the user.
Private Sub txtQuantity_AfterUpdate()
    ' Perform calculation after quantity is updated
    Me.txtTotal.Value = Me.txtQuantity.Value * Me.txtPrice.Value
End Sub

c. ComboBox Change Event

  • Trigger: Occurs when the user selects or changes the value in a combo box control.
  • Use: Ideal for dynamically updating other controls based on the selected value in the combo box.
Private Sub cboCategory_Change()
    ' Update another control based on combo box selection
    If Me.cboCategory.Value = "Electronics" Then
        Me.txtDiscount.Value = 10
    Else
        Me.txtDiscount.Value = 5
    End If
End Sub

d. Checkbox Click Event

  • Trigger: Occurs when a user clicks a checkbox control, either checking or unchecking it.
  • Use: Often used to toggle certain options or enable/disable other controls based on the checkbox status.
Private Sub chkActive_Click()
    If Me.chkActive.Value = True Then
        MsgBox "This record is now active."
    Else
        MsgBox "This record is now inactive."
    End If
End Sub

e. Option Group Click Event

  • Trigger: Occurs when the user selects a different option in an option group.
  • Use: Useful for capturing user preferences or dynamically changing form behavior based on the selected option.
Private Sub optPaymentMethod_Click()
    If Me.optPaymentMethod.Value = 1 Then
        Me.txtCreditCard.Visible = True
    Else
        Me.txtCreditCard.Visible = False
    End If
End Sub

f. ListBox or ComboBox Double-Click Event

  • Trigger: Occurs when a user double-clicks an item in a list box or combo box control.
  • Use: Ideal for taking action based on a specific selection, such as opening a detailed record or initiating a specific task.
Private Sub lstEmployees_DblClick(Cancel As Integer)
    ' Open employee details form when double-clicked
    DoCmd.OpenForm "EmployeeDetails", , , "EmployeeID = " & Me.lstEmployees.Value
End Sub

3. Handling Control Events in VBA

Control events are handled by associating specific VBA procedures with the event actions. This can be done directly in the Access interface through the Property Sheet for the control, where you select the appropriate event (such as "On Click" for a button) and write the corresponding VBA code to be executed when that event occurs.

Example: Handling a Button Click Event

  1. Open the form in Design View.
  2. Add a Command Button control to the form.
  3. Right-click the button, select Build Event, and choose On Click.
  4. The VBA editor opens where you can write the event handler:
Private Sub btnClose_Click()
    ' Close the form when the button is clicked
    DoCmd.Close
End Sub

4. Common Use Cases for Control Events

Control events are often used for tasks such as:

a. Data Validation

  • Using the After Update event of a textbox or combo box to validate user input and provide feedback.
Private Sub txtPhone_AfterUpdate()
    If Len(Me.txtPhone.Value) <> 10 Then
        MsgBox "Phone number must be 10 digits."
    End If
End Sub

b. Updating Other Controls Based on User Selection

  • In combo boxes or option groups, control events like Change or Click are used to adjust the visibility or value of other controls based on user input.
Private Sub cboCountry_Change()
    If Me.cboCountry.Value = "USA" Then
        Me.txtState.Visible = True
    Else
        Me.txtState.Visible = False
    End If
End Sub

c. Running Queries or Reports

  • Button events like On Click are used to run queries, print reports, or open other forms.
Private Sub btnGenerateReport_Click()
    DoCmd.OpenReport "SalesReport", acViewPreview
End Sub

5. Best Practices for Handling Control Events

  • Keep Code Modular: Avoid placing too much code in event handlers. Instead, call separate functions or procedures to keep the code readable and maintainable.
  • Validate User Input: Use the Before Update or After Update events to ensure that user input is valid before saving or processing it.
  • Use Error Handling: Add error handling in control event procedures to manage unexpected issues during user interactions.
  • Make Controls Responsive: Control events can be used to dynamically adjust the form based on user input, such as showing or hiding fields or updating data.

6. Conclusion

Control events in Access VBA are essential for creating interactive, user-friendly applications. By tying specific actions (e.g., button clicks, text changes, and list selections) to VBA code, you can ensure that your forms behave dynamically based on user input. Understanding how to handle control events will help you build robust Access applications that respond to user actions and make the interface more intuitive.

Commenting is not enabled on this course.