?
Solved

Run VBA when ENTER is pressed

Posted on 2011-02-25
8
Medium Priority
?
846 Views
Last Modified: 2013-11-27
Hi Experts,

I have a form with a text box named txtTicket#.  I want some code that when the Enter button on the keyboard is pressed (only if this field has focus) it will run an update query and set the field back to blank.  I know how to do everything except tell Access to run these commands only when enter is pressed.

Thanks,
Cody
0
Comment
Question by:Cody Vance
[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
  • 2
  • 2
  • +1
8 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34982886
you need to set the key Preview property of the form to YES
0
 
LVL 75
ID: 34982918
Do you mean this:

Private Sub txtYourTextBoxName_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbkeyenter Then
        KeyCode = 0
        'run your code here
    End If
End Sub

mx
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1400 total points
ID: 34982928
Typo:


Private Sub txtYourTextBoxName_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then
        KeyCode = 0
        'run your code here
    End If
End Sub
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:Cody Vance
ID: 34983134
Thanks Cap & MX.  DO I need to enable Key Preview with MXs code as well?
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 34983136
For me, I like to reserve the Enter key for it's default functionality.
I just don't like trying to "Tack on" things done when the Enter key is pressed.

I would try to find a more appropriate Event to run this code...
(Before Update event of the form, so it can be canceled if need be)

The enter key can be pressed while a user is in the textbox for any number of reasons, thus triggering your code unintentionally.

What is your plan to roll back the code if the enter key is hit by mistake, or hit more than once?
0
 
LVL 75
ID: 34983143
"DO I need to enable Key Preview with MXs code as well?"
Yes.

mx
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 600 total points
ID: 34983225
<DO I need to enable Key Preview>  That is the very first thing that you should do.

if not The CODES posted are meaningless (will not execute)
0
 

Author Comment

by:Cody Vance
ID: 34983240
Got it, thank you both.  Points to follow.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

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…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

719 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