9

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

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

完成后如何保持对这个领域的关注DoStuff()

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

6 回答 6

16

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

KeyDown → BeforeUpdate → AfterUpdate → Exit → LostFocus

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

于 2013-09-24T21:10:54.190 回答
6

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

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
于 2013-09-30T14:19:55.577 回答
1
  1. 点击access options
  2. 选择Advanced
  3. Don't move从中选择Move after enter
  4. 点击ok

它将工作 100%

于 2018-07-08T08:25:56.803 回答
1

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

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

表单模块中的代码:

    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

以及标准模块中的代码:

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

阿尔蒂克

于 2019-01-27T12:50:29.627 回答
1

尝试删除整行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
于 2018-08-01T03:42:48.300 回答
0

在 Excel 中工作的一个简单解决方案是将 KeyCode 设置为 0。如果 DoStuff 窃取了焦点,那么您还应该将焦点设置回来:

Private Sub MyFld_KeyDown(KeyCode As Integer, Shift As Integer)  
     If KeyCode = vbKeyReturn Then  
         DoStuff 
         KeyCode = 0
         Me.MyFld.SetFocus  
     End If
End Sub
于 2020-09-19T15:14:43.270 回答