Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

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.

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

Open in new window

Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try
Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn then
        Call cmdSearchContracts_Click
    End If
End Sub

Open in new window

Regards
Avatar of SteveL13

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

Open in new window

Is there a way to create a public module and call it from each field?
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect.  Thank you!