Question

vb6 how capture enter key press?

Asked by: EYoung

I have a VB 6 application in which I have several text boxes on a form.  When the user types in a number in the first text box and presses the Enter key, I want the application to set focus to the second text box, etc.

I have done this in previous application to speed up data entry, but now when I try it, it does not seem to work.  Below is the code I am using.

Private Sub txtContract_Labor_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then       'User pressed the Enter key
        txtContract_Materials.SetFocus
    End If
End Sub

This code below does not even fire off when the enter key is pressed:

Private Sub txtContract_Labor_KeyDown(KeyCode As Integer, Shift As Integer)
    MsgBox KeyCode
End Sub

Any ideas why it does not work?  Thanks.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-03-20 at 15:36:56ID21781639
Tags

vb6

,

key

,

enter

,

keypress

Topic

Visual Basic Programming

Participating Experts
3
Points
100
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Keyascii (Keycode constants)
    Using vb5. I have a msflexgrid, with records in it. I am able to delete a specific row using the keypress function, but only when i use the delete key on the numeric side of the keyboard(ie. the right hand side) It does not detect that the normal delete key situated with the ...
  2. Problems with KeyPress/KeyDown
    Hi, Sorry for the length of the question.. I have a problem using the KeyDown Method with multiple forms I have two forms (frmMain, frmSub) ---------------------------------------- frmMain CODE Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Ca...
  3. If KeyAscii = 13 Then
    after a number is typed in a text box instead of clicking a button i would like the form/textboxto respond to the enter key If KeyAscii = 13 Then cmdOk.Enabled = True End If i have tried this code within the form keypress keydown change and also the textbox change to no avail?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Idle_MindPosted on 2006-03-20 at 19:54:19ID: 16243702

It works fine for me:

Private Sub txtContract_Labor_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        KeyAscii = 0
        SendKeys "{TAB}"
    End If
End Sub

 

by: superg65Posted on 2006-03-20 at 21:16:14ID: 16244004

You might need to set the form property KeyPreview on your form to True to catch these key presses.

:)

 

by: Idle_MindPosted on 2006-03-20 at 21:41:30ID: 16244095

Yes, if you're using the Form_KeyPress() event...but it looks like he is trapping the KeyPress() event for a TextBox called "txtContract_Labor".

The form KeyPress() event (which would need KeyPreview set to True) would be:

    Private Sub Form_KeyPress(KeyAscii As Integer)

    End Sub

 

by: superg65Posted on 2006-03-20 at 23:00:12ID: 16244416

Haha Idle Mind ever lurking.

You are quite right -  KeyPreview refers to form level action - my mistake. The code supplied worked for me also - sorry I can't see anything worng with it.

:(

 

by: Mark_FreeSoftwarePosted on 2006-03-21 at 07:26:59ID: 16247586


make sure that is the actual name of your textbox!

doubleclick on it when in designerview to check this

if it is the right one you should end with the cursor in this sub

 

by: EYoungPosted on 2006-03-22 at 10:20:26ID: 16260191

Thanks everyone for the input.  I entered the KeyPress code above and it does get triggered when all keys are pressed "except" the Enter key.  (The shift, cntr, and alt keys also do not trigger the event, but I only care about knowing when the Enter key is pressed.)

I don't get this as I have used this logic before and it has worked perfectly.  The text box is on a frame which is on the form but that should not matter as the text box does have focus.  I also did confirm the name of the text box by double clicking in design view mode and it is the correct name.

I have set a breakpoint at the KeyPress line and it is not triggered when the Enter key is pressed.  It is only triggered when the numeric and alpha keys along with the + and - signs are pressed.

Any ideas?  Thanks.

 

by: Mark_FreeSoftwarePosted on 2006-03-22 at 10:27:06ID: 16260247


make sure the enter key is not broken!

if that isn't the problem, try posting the settings of the textbox that is giving the problem

also post if it is the intrinsic one or one from forms 2.0 or a rtf etc.....


also Enter is not an english word (as far as i know), i think you mean Return

 

by: EYoungPosted on 2006-03-22 at 10:39:00ID: 16260363

1.  The Enter key is not broken.  I tested it in another applicaton I wrote and it works the way it is suppose to.

2.  I use the term Enter and that is what is printed on the key.

3.  Good question about the intrinsic one or one from Forms 2.0 or rtf etc.  It is the intrinsic one.  In fact, I copied a new textbox to the form and tried it and it does not work either.  Very strange.

4.  Below are the settings of the textbox:  

txtContract_Labor TextBox

Appearance Flat
BorderStyle 1-Fixed Single
HideSelection True

CausesValidation True

 

by: EYoungPosted on 2006-03-22 at 10:43:23ID: 16260405

4.  Below are the settings of the textbox:  

txtContract_Labor TextBox

Appearance Flat
BorderStyle 1-Fixed Single
HideSelection True

CausesValidation True
Enabled True
Locked False
MaxLength 20
MultiLine False
ScrollBars 0-None
TabIndex 41
TabStop True
Visible True

Name txtContract_Labor
Alignment 1-Right Justify
HelpContextID 0
Index
MouseIcon (None)
MousePointer 0-Default
PasswordChar
Tag
Text
ToolTipText
WhatThisHelpID 0


I did not list all of the properties.

Thanks for the help

 

by: Mark_FreeSoftwarePosted on 2006-03-22 at 10:47:21ID: 16260445


try putting your code in the keyUp event, and replace keyascii with KeyCode

does that work?


is there any subclassing in your app that is catching keys?

 

by: Idle_MindPosted on 2006-03-22 at 10:47:39ID: 16260448

Try a new project with just a textbox (no frames or anything else) and the code.  

Does the problem persist?

 

by: EYoungPosted on 2006-03-22 at 10:52:01ID: 16260499

I found part of the problem.  I have on the form some command buttons.  One of them (the "Add" button) I had set the Default property to True.  When I changed it to False, then when I press the Enter key on the txtContract_Labor control, the KeyPress event does work correctly.

Possibly contributing further to the problem is the fact that I have set up some automatic help text that triggers whenever the cursor moves over any control on the form.  I user each control's MouseMove event.  That might be causing the txtControl_Labor text box to loose control?  That does not seem to be happening but I can't figure out why setting the Default property on the "Add" command button from True to False allows the KeyPress event to work on the text box.

Thanks for the input.

 

by: Mark_FreeSoftwarePosted on 2006-03-22 at 11:00:53ID: 16260606


no it is the default property of the button


that chatches the enter key

 

by: EYoungPosted on 2006-03-22 at 11:05:47ID: 16260656

I don't get that.  Does setting the command button's default property from False to True mean that anytime the enter key is pressed anywhere on the form, the command button is triggered?  I guess that makes sense.

Thanks for all the help.  I am going to increase the points and split them.

 

by: Mark_FreeSoftwarePosted on 2006-03-22 at 11:08:35ID: 16260686

I don't get that.  Does setting the command button's default property from False to True mean that anytime the enter key is pressed anywhere on the form, the command button is triggered?

that's true
try setting a messagebox in the command_click of that particular commandbutton and is should fire


in fact this is the same with the cancel property, but then for the escape key

 

by: EYoungPosted on 2006-03-22 at 11:16:46ID: 16260767

Yes.  The msgbox did fired off when I pressed the Enter key while the text box had focus.  I guess that makes sense - kind of...

Thanks to both of you for the help.  I think everything is fine now.  Whew...  I am off and running now with the project.

 

by: Mark_FreeSoftwarePosted on 2006-03-22 at 11:27:51ID: 16260884


thanx for the points and happy coding!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...