MsgBox 之后,VBA 忽略将焦点设置为 ComboBox

问题描述 投票:0回答:2

在消息框回答后,我希望用户被引导回组合框“aceCount”。然而,它似乎完全跨越了这条线。在下面的代码中,我有两个消息框“代码正在运行”和“焦点后”;两者都出现了,所以我知道它正在读取该区域的代码,它只是字面上忽略了 setfocus 部分。我已经阅读了所有可以阅读的文章,并尝试了其他问题的多种解决方案,但无济于事。它还不会将焦点设置到消息框之后表单上的任何内容。我尝试过文本框和组合框。

编辑:它似乎将焦点集中在我需要的组合框上,但随后执行到下面粗体的 End If 并运行代码。它不给用户重新提交的机会。

 Private Sub SubmitButton_Click()
'Calls all necessary subs to perform required actions. If you add a sub, you     will have to call it here if you want it to perform when you submit the form.
'If you have requested any number of HA Oracle servers, checks also if ACE and F5 are empty:
    If Me.oracleCount.ListIndex > 0 Then

'If Ace and F5 are empty, MessageBox requesting review.
    If aceCount.Value = 0 And F5Count.Value = 0 Then
       If MsgBox("You have requested a HA Oracle server, but no Ace or F5. Do you need ACE or F5?", vbYesNo + vbQuestion, "ACE or F5?") = vbYes Then
'If chooses yes, goes back to form to allow user to input data
     Cancel = True
     MsgBox "code working"
     UserForm1.aceCount.SelStart = 0
     MsgBox "after focus"
'If chooses no, submits code as normal
       Else

        Call EnterData
        Call HideSheets
        Unload Me

    **End If**
'If value in either Ace or F5, submits code as normal
   ElseIf (Me.aceCount.Value <> 0 Or Me.F5Count.Value <> 0) Then
        Call EnterData
        Call HideSheets
        Unload Me

  End If
  Else
  End If
  Call EnterData
  Call HideSheets
  Unload Me

结束子

vba combobox setfocus msgbox
2个回答
0
投票

您不使用

SetFocus
UserForm1.aceCount.SelStart = 0
行未将焦点设置到 UserForm1.aceCount。如果你想将焦点设置到该控件,你应该写
UserForm1.aceCount.SetFocus

如果 UserForm1 是您当前表单的子表单,您可以使用

UserForm1.Form.aceCount.SetFocus
UserForm1.SetFocus

0
投票

我希望这封信在 7 年后让你感觉良好......,

我刚刚在调查我的表单中的类似问题,发现当使用像

_Exit
_AfterUpdate
这样的事件时,它们会通过将焦点操作从文本框等元素上移开而被触发,这将成为绑定操作,直到执行事件代码。我相信它们与
Cancel = True
相似。

因此,

.Setfocus
.SelStart = 0
属性/方法按预期工作,但在设置焦点后,应用程序继续执行将焦点移开的绑定操作!...

因此,我通过创建全局布尔变量来在发生更改时保存值来解决这个问题,该变量在 _Change 事件中设置, 然后,我为所有其他事情添加了

_Enter
_Click
事件,我不希望用户将焦点设置到存在有效输入之前,并在开始时添加代码以检查布尔变量值。

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.