目标:使用第一个命令按钮的GotFocus过程将焦点从一个命令按钮重定向到另一个命令按钮。
上下文:我在一个通用模块中有一个与表单无关的过程,在大多数表单上,它在保存前一条记录后将焦点放在NewRecord按钮上。但是在一种形式上,我希望(基于某些条件)将焦点重定向回SignRecord按钮,这样用户就可以“签署”同一记录的第二部分(将来我可能需要它用于其他用途)。目标控件已启用且可见,否则可被聚焦,并且在未发生重定向时可聚焦原始控件。下面的引用2暗示这应该是可能的,尽管我没有改变我的控件的可见性。
问题:当满足在GotFocus过程中重定向焦点的条件时,它会根据需要进行重定向,但原始(测试) SetFocus调用会抛出“运行时错误'2110',无法将焦点移到控件CommandNew”。
我尝试过的:
在我的下游SetFocus调用之后执行Exit Sub。
Call CommandSign.SetFocus,希望它能在以前的SetFocus进程之外实现。
在模块中,
Public Sub test()
Forms("TargetForm").CommandNew.SetFocus 'This gets the error '2110'
End Sub在“TargetForm”中
Private Sub CommandNew_GotFocus()
If IsNull(textDateTime) Then Exit Sub 'Works as expected
'I can see these two parts work. The framSign value changes
'and CommandSign gets focus
If checPPC And IsNull(textSigID_PPC) And framSign = 2 Then
framSign = 1
CommandSign.SetFocus
ElseIf checDAS And IsNull(textSigID_DAS) And framSign = 1 Then
framSign = 2
CommandSign.SetFocus
End If
End Sub参考文献:
1:SelectNextControl() a bad idea in a GotFocus event?
2:http://www.access-programmers.co.uk/forums/showthread.php?t=100071
发布于 2013-11-19 01:58:17
我认为您的问题是,实际上,在Private Sub CommandNew_GotFocus()完成执行之前,对Forms("TargetForm").CommandNew.SetFocus的调用似乎并没有完全完成将焦点设置为CommandNew。因为您在第一个SetFocus可能完成之前调用了另一个SetFocus,所以Access似乎无法处理冲突。
不管是不是这样,有一件事是明确的:不幸的是,你现在设置执行计划的方式是行不通的。您可以尝试向每个窗体添加全局变量或公共变量,以确定在将焦点设置为CommandNew之后是否应将焦点设置为CommandSign。
例如。TargetForm:
Public boolSetCommandSignFocusInstead As Boolean
Private Sub CommandNew_GotFocus()
If IsNull(textDateTime) Then Exit Sub 'Works as expected
'I can see these two parts work. The framSign value changes
'and CommandSign gets focus
If checPPC And IsNull(textSigID_PPC) And framSign = 2 Then
framSign = 1
boolSetCommandSignFocusInstead = True
ElseIf checDAS And IsNull(textSigID_DAS) And framSign = 1 Then
framSign = 2
boolSetCommandSignFocusInstead = True
Else
boolSetCommandSignFocusInstead = False
End If
End Sub模块:
Public Sub test()
Forms("TargetForm").CommandNew.SetFocus
If Forms("TargetForm").boolSetCommandSignFocusInstead Then
Forms("TargetForm").CommandSign.SetFocus
End If
End Subhttps://stackoverflow.com/questions/17346025
复制相似问题