2

目标:使用第一个GotFocus过程将焦点从一个命令按钮重定向到另一个命令按钮。

上下文:我在一个通用模块中有一个独立于表单的过程,在大多数表单上,它在保存上一条记录后将焦点设置到 NewRecord 按钮。但是在一种形式上,我想(基于某些条件)将焦点重定向回 SignRecord 按钮,以便用户可以“签署”同一记录的第二部分(我将来可能需要它用于其他用途)。目标控件已启用且可见,并且可以在不发生重定向时获得焦点,并且可以将原始控件获得焦点。下面的参考 [2] 暗示这应该是可能的,尽管我没有改变我的控件的可见性。

问题:当满足在GotFocus过程中重定向焦点的条件时,它会根据需要进行重定向,但原始(测试)SetFocus调用会引发“运行时错误 '2110',无法将焦点移动到控件 CommandNew”。

我试过的:
Exit Sub在我的下游SetFocus电话之后。
Call CommandSign.SetFocus希望它能在之前的SetFocus过程之外发生。

在一个模块中,

Public Sub test()
    Forms("TargetForm").CommandNew.SetFocus 'This gets the error '2110'
End Sub

在“目标表单”中,

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() 在 GotFocus 事件中是个坏主意?
[2]:http ://www.access-programmers.co.uk/forums/showthread.php?t=100071

4

1 回答 1

2

认为您的问题是,实际上,调用Forms("TargetForm").CommandNew.SetFocus似乎并没有完成将焦点设置为CommandNew直到Private Sub CommandNew_GotFocus()完成执行之后。因为您在第一个 SetFocus 完成之前调用了另一个 SetFocus,所以存在 Access 似乎无法处理的冲突。

不管是不是这样,有一件事很清楚:不幸的是,你现在设置执行计划的方式是行不通的。您可以尝试将全局变量或公共变量添加到每个表单,以确定在将焦点设置为CommandSign 是否应将焦点设置为CommandNew

前任。目标形式:

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 Sub
于 2013-11-18T17:58:16.540 回答