活动控件更改事件-MS Access [英] Active Control Change Event - MS Access

查看:76
本文介绍了活动控件更改事件-MS Access的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个MS-Access表单事件,该事件可以检查表单上的活动控件是否已更改为另一个控件.当它运行一个小脚本时.

I'm looking for a MS-Access form event which can check if the active control on the form has changed to another control; when it does a small script runs.

该功能必须是仅在窗体处于活动状态(例如,单击窗体等)时才运行的功能.但是,Form_Click()无法正常工作,因为它在某种程度上不是同一窗口. Form_Click()也仅在单击表单而不是控件(例如记录选择器")时才起作用.此方法应适用于所有控件的一种方法,而不是每个控件一种方法.

The function must be one which runs only when the form is active (such as a click on the form, etc). However, Form_Click() doesn't work as it somehow is not the same window.. I don't know what's going on there. Form_Click() also only works if you click form pieces, not controls (such as the Record Selector). This method should work for all controls with one method, not one method per control.

我的代码:

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 a way to do this? I can't use timers as if the user navigates away from the form, the Me.ActiveControl is no longer in the window and throws an error. Or, if anybody knows a way to check:

If (Me.Form IS IN ACTIVE WINDOW) Then ....

推荐答案

您可以使用WithEvents通过类模块进行此操作.不幸的是,没有任何事件附加到通用Control对象,因此您将必须为每种不同类型的控件指定一个处理程序.我提供了三种常用控件来帮助您入门.

You could do this via a class module using WithEvents. Unfortunately, there are no events attached to the generic Control object, so you will have to specify a handler for each different type of control. I've included three common controls to get you started.

创建一个名为weControlChange的新类模块,并将以下代码粘贴到其中.然后按照类模块顶部的用法注释进行实施.

Create a new class module named weControlChange and paste the following code into it. Then follow the usage comments at the top of the class module to implement.

' 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

这篇关于活动控件更改事件-MS Access的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆