Active Management Change Event - MS Access

I am looking for an MS-Access form event that can check if the active control in the form has been changed to another control; when he runs a small run script.

The function should be the one that runs only when the form is active (for example, clicking on the form, etc.). However, Form_Click () does not work, because it is somehow not the same window. I don’t know what is going on there. Form_Click () also only works if you click the curly parts, and not the controls (such as the record selector). This method should work for all controls with one method, not one method for each control.

my code is:

Private Sub <<Form_ActiveHasChanged()>>
  desc = Forms(Me.Form.Name).Controls(Me.ActiveControl.Name).StatusBarText
  Me.txtInfo.Caption = desc
End Sub

where <<Form_ActiveHasChanged()>>is my event .. is there any way to do this? I can not use timers, as if the user was moving from the form, Me.ActiveControl is no longer in the window and throws an error. Or, if anyone knows a way to check:

If (Me.Form IS IN ACTIVE WINDOW) Then ....
+3
source share
3 answers

You can do this through the class module with WithEvents. Unfortunately, there are no events related to the shared object Control, so you will need to specify a handler for each type of control. I have included three common controls to get you started.

Create a new class with the name weControlChangeand paste the following code into it. Then follow the usage comments at the top of the class module for implementation.

' Usage: 1. Add the following to the declaration section of the form module:
'               Dim ControlChange As New weControlChange
'        2. Add the following to the Form_Load OR Form_Open event:
'               ControlChange.Setup Me.Form
Option Compare Database
Option Explicit

Private WithEvents weTextBox As TextBox
Private WithEvents weComboBox As ComboBox
Private WithEvents weCheckBox As CheckBox

Private CtlColl As Collection

Public Sub Setup(Frm As Form)
Dim Ctl As Control, CtlChng As weControlChange
    Set CtlColl = New Collection
    For Each Ctl In Frm.Section(acDetail).Controls 
   'For Each Ctl In Frm.Controls    ''to include controls from all sections'
        Select Case Ctl.ControlType
        Case acTextBox, acComboBox, acCheckBox
            If Ctl.Enabled And Ctl.Visible Then
                Set CtlChng = New weControlChange
                Set CtlChng.Control = Ctl
                CtlColl.Add CtlChng
            End If
        End Select
    Next Ctl
End Sub

Public Property Set Control(ByVal Ctl As Control)
    Select Case Ctl.ControlType
    Case acTextBox
        Set weTextBox = Ctl
        weTextBox.OnEnter = "[Event Procedure]"
    Case acComboBox
        Set weComboBox = Ctl
        weComboBox.OnEnter = "[Event Procedure]"
    Case acCheckBox
        Set weCheckBox = Ctl
        weCheckBox.OnEnter = "[Event Procedure]"
    End Select
End Property

Private Sub weCheckBox_Enter()
    MyScript weCheckBox
End Sub

Private Sub weComboBox_Enter()
    MyScript weComboBox
End Sub

Private Sub weTextBox_Enter()
    MyScript weTextBox
End Sub

Private Sub MyScript(Ctl As Control)
    'Your code goes here
End Function

Private Sub Class_Terminate()
Dim Ctl As Object
On Error Resume Next
    If Not CtlColl Is Nothing Then
        For Each Ctl In CtlColl
            Set Ctl = Nothing
        Next Ctl
        Set CtlColl = Nothing
    End If
End Sub
+3
source

, , , OnExit . Ctrl-A, , "" OnExit. 20 .

+2

The easiest way to do this is to use the OnEnter event handler for each control.

0
source

All Articles