Solved

Run VBA when ENTER is pressed

Posted on 2011-02-25
8
785 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:codyvance1
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
you need to set the key Preview property of the form to YES
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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 - Access MVP) earned 350 total points
Comment Utility
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
 

Author Comment

by:codyvance1
Comment Utility
Thanks Cap & MX.  DO I need to enable Key Preview with MXs code as well?
0
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!

 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
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

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
"DO I need to enable Key Preview with MXs code as well?"
Yes.

mx
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 150 total points
Comment Utility
<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:codyvance1
Comment Utility
Got it, thank you both.  Points to follow.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 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

11 Experts available now in Live!

Get 1:1 Help Now