SteveL13
asked on
How run code when Enter key is pressed
I have this code in an onclick event of a command button on a form. But I would also like the same code to run when the user presses the Enter key after making an entry in any field on the form. How can this be done?
Here is the onclick code of the command button.
Here is the onclick code of the command button.
Private Sub cmdSearchContracts_Click()
Dim strWhereCondition As String
'lots of code here (removed for this demonstration) to establish the strWhereCondition
If Right(strWhereCondition, 5) = " AND " Then strWhereCondition = Left(strWhereCondition, Len(strWhereCondition) - 5)
Me.Parent.subfrmSearchContractsDS.Form.Filter = strWhereCondition
Me.Parent.subfrmSearchContractsDS.Form.FilterOn = True
Me.Parent.subfrmSearchContractsDS.Form.Visible = True
End Sub
ASKER
I added that code to the form VBA and it does not work. Nothing happens when I press the Enter key.
then use Keypress in all the fields
Private Sub Text0_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
cmdSearchContracts_Click
End If
End Sub
ASKER
Is there a way to create a public module and call it from each field?
You could create a class module an example
https://social.msdn.microsoft.com/Forums/office/en-US/3154a4c4-53c9-431a-b2e2-5ede873bd60a/extending-textbox-with-a-class-module?forum=accessdev
https://social.msdn.microsoft.com/Forums/office/en-US/3154a4c4-53c9-431a-b2e2-5ede873bd60a/extending-textbox-with-a-class-module?forum=accessdev
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect. Thank you!
pls try
Open in new window
Regards