Solved

How run code when Enter key is pressed

Posted on 2016-08-04
7
41 Views
Last Modified: 2016-08-04
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

0
Comment
Question by:SteveL13
  • 3
  • 3
7 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41742459
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
0
 

Author Comment

by:SteveL13
ID: 41742469
I added that code to the form VBA and it does not work.  Nothing happens when I press the Enter key.
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41742485
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

0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:SteveL13
ID: 41742508
Is there a way to create a public module and call it from each field?
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41742527
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 41742771
<<Is there a way to create a public module and call it from each field?>>

Yes and you don't really need a class module.

But the simplest solution is to set the Default property to true on the command button.

The button will be "clicked" whenever a user presses enter on the form.

Jim.
0
 

Author Closing Comment

by:SteveL13
ID: 41743016
Perfect.  Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now