Press enter in textbox and execute button function in VBA

前端 未结 5 879
春和景丽
春和景丽 2020-12-11 06:23

I have a login form to my database done in Access 2010 and using VBA code. I want to be able to press Enter on txtboxPassword and automatically execu

相关标签:
5条回答
  • 2020-12-11 06:54

    KeyPress rather than KeyDown works for me, and calling the _Click sub avoids sending triggers that may fire other events.

    Private Sub txtboxPassword_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then
             Call btnLogin_Click
        End If
    End Sub
    
    0 讨论(0)
  • 2020-12-11 06:58

    The buttons in Access have a property called Default (on the Other property page). If you set it to Yes the form calls the button click event handler automatically, when you press Enter. No need for additional Key-event handling.

    There is also a Cancel property. If you set it to Yes for a button, the form activates it automatically when the user types the Esc-key. Very practical for Cancel buttons.

    0 讨论(0)
  • 2020-12-11 06:58

    Cant reproduce exactly your problem, but some time ago I had somewhat similar issue and solved it by adding:

    Private Sub txtboxPassword_KeyDown(KeyCode As Integer, Shift As Integer)
      If KeyCode = 13 Then
         btnLogin_Click
         KeyCode.Value = 0
      End If
    End Sub
    
    0 讨论(0)
  • 2020-12-11 06:59

    On the button you wish to action go to properties 'Other' and set the 'Default' to Yes. Then when you click on enter when in the text box it will action the button.

    0 讨论(0)
  • 2020-12-11 07:13

    to activate KeyCode, you need to set Key Preview Property on form to YES (it will be at the bottom)

    If KeyCode = vbKeyReturn Then
        Your_fuction_here
    End If
    
    0 讨论(0)
提交回复
热议问题