Access VBA 停止显示 ActiveControl 是焦点控件
Access VBA Stopped showing ActiveControl Is the focused control
我有一点古怪的问题。我使用例程来检查焦点;并且......它停止工作。我不知道如何或为什么。
该例程主要检查活动控件是否是您正在检查的控件,如果是,return为真(因此我们可以处理不是的情况)。
但是...它最近开始一直 returning false(我们没有做任何更改,我们只注意到某些字段审计开始时 returning 奇怪的值)。即使当控件获得焦点时,如果窗体上没有其他控件,或者只打开一个窗体,并且窗体显然有焦点。
有没有人知道这是怎么回事或为什么会这样?这让我很困惑。正如您所看到的,我有一个测试字段,我们在其中 运行 一个初始化...并且值明显匹配,名称,值,每个字段都进行了比较,但它仍然没有 return 是的。
我做错了什么?
编辑:忘记添加代码。
一切照原样:
' I call it from here:
' Inside form, any control, say `PurchaseCostBox`
Private Sub PurchaseCostBox_AfterUpdate()
' Check if the focus is had
If VerifyFocus(Me.PurchaseCostBox) Then
' Save more field info.
Debug.Print Me.PurchaseCostBox.SelStart
Debug.Print Me.PurchaseCostBox.SelLen
Debug.Print Me.PurchaseCostBox.Value
Else
' Do limited stuff
Debug.Print Me.PurchaseCostBox.Value
End if
End Sub
Public Function VerifyFocus(ByRef ctlWithFocus As Control) As Boolean
Dim FrmParent As Form
Dim ctlCurrentFocus As Control
On Error Resume Next
' Determine parent form for control
' Verify focus of parent form
Set FrmParent = Screen.ActiveForm
' Verify focus of control on form
Set ctlCurrentFocus = FrmParent.ActiveControl
If Not ctlCurrentFocus Is ctlWithFocus Then
ctlWithFocus.SetFocus
DoEvents
End If
' Even adding the below line does not return true:
ctlWithFocus.SetFocus
' Return true if the control currently has the focus
VerifyFocus = FrmParent.ActiveControl Is ctlWithFocus
' Discard any errors
Err.Clear
End Function
我也试过这个:
Public Function VerifyFocus(ByRef ctlWithFocus As Control) As Boolean
On Error Resume Next
' Return true if the control currently has the focus
VerifyFocus = Screen.ActiveControl Is ctlWithFocus
' Discard any errors
Err.Clear
End Function
不再工作了...我正在挣扎。
好吧,结果是完全出乎意料的事情,与焦点完全无关。
事实证明,我调用它的方法之一是通过使用 Control.Properties.Parent.Form
获取控件的父级。虽然这 return 是正确的形式,但它也使上面的 VerifyFocus
例程永远不会 return 正确(即使它没有被使用)。我不知道为什么。在这一点上,我真的不在乎。但是我会把它留在这里让其他人找到。
重构我的 GetTopForm
例程让我得到了关注。
我有一点古怪的问题。我使用例程来检查焦点;并且......它停止工作。我不知道如何或为什么。
该例程主要检查活动控件是否是您正在检查的控件,如果是,return为真(因此我们可以处理不是的情况)。
但是...它最近开始一直 returning false(我们没有做任何更改,我们只注意到某些字段审计开始时 returning 奇怪的值)。即使当控件获得焦点时,如果窗体上没有其他控件,或者只打开一个窗体,并且窗体显然有焦点。
有没有人知道这是怎么回事或为什么会这样?这让我很困惑。正如您所看到的,我有一个测试字段,我们在其中 运行 一个初始化...并且值明显匹配,名称,值,每个字段都进行了比较,但它仍然没有 return 是的。
我做错了什么?
编辑:忘记添加代码。 一切照原样:
' I call it from here:
' Inside form, any control, say `PurchaseCostBox`
Private Sub PurchaseCostBox_AfterUpdate()
' Check if the focus is had
If VerifyFocus(Me.PurchaseCostBox) Then
' Save more field info.
Debug.Print Me.PurchaseCostBox.SelStart
Debug.Print Me.PurchaseCostBox.SelLen
Debug.Print Me.PurchaseCostBox.Value
Else
' Do limited stuff
Debug.Print Me.PurchaseCostBox.Value
End if
End Sub
Public Function VerifyFocus(ByRef ctlWithFocus As Control) As Boolean
Dim FrmParent As Form
Dim ctlCurrentFocus As Control
On Error Resume Next
' Determine parent form for control
' Verify focus of parent form
Set FrmParent = Screen.ActiveForm
' Verify focus of control on form
Set ctlCurrentFocus = FrmParent.ActiveControl
If Not ctlCurrentFocus Is ctlWithFocus Then
ctlWithFocus.SetFocus
DoEvents
End If
' Even adding the below line does not return true:
ctlWithFocus.SetFocus
' Return true if the control currently has the focus
VerifyFocus = FrmParent.ActiveControl Is ctlWithFocus
' Discard any errors
Err.Clear
End Function
我也试过这个:
Public Function VerifyFocus(ByRef ctlWithFocus As Control) As Boolean
On Error Resume Next
' Return true if the control currently has the focus
VerifyFocus = Screen.ActiveControl Is ctlWithFocus
' Discard any errors
Err.Clear
End Function
不再工作了...我正在挣扎。
好吧,结果是完全出乎意料的事情,与焦点完全无关。
事实证明,我调用它的方法之一是通过使用 Control.Properties.Parent.Form
获取控件的父级。虽然这 return 是正确的形式,但它也使上面的 VerifyFocus
例程永远不会 return 正确(即使它没有被使用)。我不知道为什么。在这一点上,我真的不在乎。但是我会把它留在这里让其他人找到。
重构我的 GetTopForm
例程让我得到了关注。