vba 为什么我的 .setfocus 被忽略?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18990437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:42:35  来源:igfitidea点击:

Why is my .setfocus ignored?

vbams-accessaccess-vbams-access-2010

提问by PowerUser

I have an Access form with a textbox that is meant to allow for repeatedly typing a number, hitting enter, and letting a script do stuff. For speed, the field should keep the focus after DoStuff()is done.

我有一个带有文本框的 Access 表单,旨在允许重复输入数字、按 Enter 键并让脚本执行操作。为了速度,该领域应该在DoStuff()完成后保持焦点。

However, while I'm sure that DoStuff()is run, the focus always goes to the next field in the tab order. It's like Me.MyFld.SetFocusis being ignored.

但是,虽然我确定它DoStuff()已运行,但焦点始终会转到 Tab 键顺序中的下一个字段。就像Me.MyFld.SetFocus被忽视了一样。

How do I keep the focus on this field after DoStuff()is done?

完成后如何将注意力集中在这个领域DoStuff()

Private Sub MyFld_KeyDown(KeyCode As Integer, Shift As Integer)  
     If KeyCode = vbKeyReturn Then  
         DoStuff  
         Me.MyFld.SetFocus  
     End If
End Sub

回答by techturtle

If you look at the order of events for a keypress that would change focus, you can see that it always follows this pattern:

如果您查看会改变焦点的按键事件的顺序,您会发现它始终遵循以下模式:

KeyDown → BeforeUpdate → AfterUpdate → Exit → LostFocus

You can re-set the focus anywhere in there and it will still keep following the pattern. So we need to tell it to stop following the pattern. Replace your Me.MyFld.SetFocuswith DoCmd.CancelEventand it should fix your problem. Basically, this just kicks you out of the above pattern, so the Exitand LostFocusevents never fire...

你可以在那里的任何地方重新设置焦点,它仍然会遵循模式。所以我们需要告诉它停止遵循这种模式。替换你Me.MyFld.SetFocusDoCmd.CancelEvent,它应该可以解决你的问题。基本上,这只会让您摆脱上述模式,因此ExitLostFocus事件永远不会触发......

回答by Johan van der Slikke

A workaround is moving the focus to another control and then back to the first control. Like this:

解决方法是将焦点移到另一个控件,然后再移回第一个控件。像这样:

Private Sub MyFld_KeyDown(KeyCode As Integer, Shift As Integer)  
    If KeyCode = vbKeyReturn Then  
        DoStuff
        Me.anotherControl.SetFocus
        Me.MyFld.SetFocus  
    End If
End Sub

回答by user10048738

  1. click on access options
  2. select Advanced
  3. select Don't movefrom Move after enter
  4. click ok
  1. 点击 access options
  2. 选择 Advanced
  3. 选择Don't moveMove after enter
  4. 点击 ok

It will work 100%

它将工作 100%

回答by Steven McCaffrey

Try removing the whole line for variable_name.SetFocusand simply add: Cancel = True

尝试删除整行variable_name.SetFocus并简单地添加: Cancel = True

Private Sub MyFld_KeyDown(KeyCode As Integer, Shift As Integer)  
     If KeyCode = vbKeyReturn Then  
         DoStuff  
         Cancel = True  
     End If
End Sub

回答by Artik

Another solution to the problem that I use in Excel.

我在 Excel 中使用的问题的另一个解决方案。

Let there exist UserForm1 with the TextBox1 and CommandButton1 controls.

让存在带有 TextBox1 和 CommandButton1 控件的 UserForm1。

Code in the form module:

表单模块中的代码:

    Option Explicit

    Private Sub CommandButton1_Click()
      Unload Me
    End Sub


    Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

      If KeyCode = vbKeyReturn Then

        'Call DoStuff

        Application.OnTime Now, "'Control_SetFocus """ & Me.Name & """, """ & Me.ActiveControl.Name & """ '"
' The concatenation returns a string:  'Control_SetFocus "UserForm1", "TextBox1"'
      End If

    End Sub

And code in the standard module:

和标准模块中的代码:

Option Explicit

Sub Control_SetFocus(FormName As String, ControlName As String)
    Dim oUserForm   As Object

    Set oUserForm = GetFormByName(FormName)

    If Not oUserForm Is Nothing Then
        oUserForm.Controls(ControlName).SetFocus
    End If
End Sub


Function GetFormByName(FormName As String) As Object
    Dim oUserForm   As Object
    On Error GoTo ErrHandle

    For Each oUserForm In VBA.UserForms
        If StrComp(oUserForm.Name, FormName, vbTextCompare) = 0 Then
            Exit For
        End If
    Next oUserForm

    If oUserForm Is Nothing Then
        Set oUserForm = UserForms.Add(FormName)
    End If

    Set GetFormByName = oUserForm
    Exit Function
ErrHandle:
    Select Case Err.Number
        Case 424:
            MsgBox "Userform " & FormName & " not exists.", vbExclamation, "Get userform by name"
        Case Else:
            MsgBox Err.Number & ": " & Err.Description, vbCritical, "Get userform by name"
    End Select

End Function

Artik

阿蒂克