?
Solved

How run code when Enter key is pressed

Posted on 2016-08-04
7
Medium Priority
?
52 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 52

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 52

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Expert Comment

by:Rgonzo1971
ID: 41742527
0
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 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

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.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

771 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