?
Solved

vb6 how capture enter key press?

Posted on 2006-03-20
17
Medium Priority
?
50,946 Views
Last Modified: 2011-08-18
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.
0
Comment
Question by:EYoung
  • 6
  • 6
  • 3
  • +1
17 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 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
0
 
LVL 2

Expert Comment

by:superg65
ID: 16244004
You might need to set the form property KeyPreview on your form to True to catch these key presses.

:)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 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
0
Industry Leaders: 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!

 
LVL 2

Expert Comment

by:superg65
ID: 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.

:(
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 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
0
 
LVL 7

Author Comment

by:EYoung
ID: 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.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 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

0
 
LVL 7

Author Comment

by:EYoung
ID: 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
0
 
LVL 7

Author Comment

by:EYoung
ID: 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
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 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?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16260448
Try a new project with just a textbox (no frames or anything else) and the code.  

Does the problem persist?
0
 
LVL 7

Author Comment

by:EYoung
ID: 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.
0
 
LVL 13

Accepted Solution

by:
Mark_FreeSoftware earned 200 total points
ID: 16260606

no it is the default property of the button


that chatches the enter key
0
 
LVL 7

Author Comment

by:EYoung
ID: 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.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 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
0
 
LVL 7

Author Comment

by:EYoung
ID: 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.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16260884

thanx for the points and happy coding!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

862 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