为什么忽略我的.setfocus?

时间:2013-09-24 19:32:39

标签: vba ms-access access-vba ms-access-2010

我有一个带有文本框的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

6 个答案:

答案 0 :(得分:14)

如果你看the order of events for a keypress that would change focus,你会发现它总是遵循这种模式:

KeyDown → BeforeUpdate → AfterUpdate → Exit → LostFocus

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

答案 1 :(得分:5)

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

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

答案 2 :(得分:1)

  1. 点击access options
  2. 选择Advanced
  3. Don't move中选择Move after enter
  4. 点击ok

它将100%工作

答案 3 :(得分: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

Artik

答案 4 :(得分:0)

尝试删除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

答案 5 :(得分: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