Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Run VBA when ENTER is pressed

Posted on 2011-02-25
8
Medium Priority
?
873 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
  • 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

886 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