3

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

该功能必须是仅在表单处于活动状态(例如单击表单等)时运行的功能。但是,Form_Click() 不起作用,因为它在某种程度上不是同一个窗口。我不知道那里发生了什么。Form_Click() 也仅在您单击表单片段而不是控件(例如记录选择器)时才有效。此方法应适用于所有具有一种方法的控件,而不是每个控件一种方法。

我的代码:

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

我的活动在哪里<<Form_ActiveHasChanged()>>..有没有办法做到这一点?我不能使用计时器,就好像用户离开表单一样,Me.ActiveControl 不再在窗口中并引发错误。或者,如果有人知道检查方法:

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

3 回答 3

3

您可以通过使用WithEvents. 不幸的是,没有附加到通用Control对象的事件,因此您必须为每种不同类型的控件指定一个处理程序。我已经包含了三个常用控件来帮助您入门。

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

' 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
于 2012-05-18T20:23:02.873 回答
2

您可以创建单个事件处理程序,将其存储在模块中,然后简单地将其设置为每个控件的 OnExit 事件。只需 Ctrl-A 将它们全部选中,打开“属性”对话框,然后设置 OnExit 事件。大约需要 20 秒。

于 2012-05-18T21:04:30.337 回答
0

最简单的方法是对每个控件使用 OnEnter 事件处理程序。

于 2012-05-21T07:27:02.310 回答