Solved

How run code when Enter key is pressed

Posted on 2016-08-04
7
39 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 48

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 48

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
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 …
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now