Solved

How run code when Enter key is pressed

Posted on 2016-08-04
7
45 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 50

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 50

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
control to hold a single value in a report. (access 2002) 7 39
Database maintenance 36 101
MS Access, How to create variable 9 37
VBA Delete sql "Too Few Parameters" 8 23
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

749 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