我正在寻找一个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 ....
答案 0 :(得分: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
答案 1 :(得分:2)
您可以创建单个事件处理程序,将其存储在模块中,然后简单地将其设置为单独的每个控件的OnExit事件。只需按Ctrl-A即可全部选中它们,打开“属性”对话框,然后设置OnExit事件。大约需要20秒。
答案 2 :(得分:0)
最简单的方法是为每个控件使用OnEnter事件处理程序。