Solved

Run VBA when ENTER is pressed

Posted on 2011-02-25
8
803 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 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 350 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
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)

 

Author Comment

by:codyvance1
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 150 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:codyvance1
ID: 34983240
Got it, thank you both.  Points to follow.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
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…

828 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