|
-
Dec 8th, 2004, 08:04 AM
#1
Thread Starter
Hyperactive Member
Using vbKeyReturn in KeyPress Event of a TextBox [RESOLVED]
I have a VBapp with a textbox that the user enters numbers into. If they press the return key, I want a sub called. The following code works fine, except for one thing. When the return key is pressed, I get a "ding" sound indicating an error (although the textbox still retains the number entered). I put in an error handler and noticed that an error is generated with each key stroke. The error no. returned is "0" and there is no error description. Can anyone help me to get rid of this annoying bug?
Code:
Private Sub txtEdit_KeyPress(KeyAscii As Integer)
'Each keystroke generates an error - text is still put into textbox
'On Error GoTo errhandler
'errhandler:
'MsgBox Err.number & " - " & Err.Description
Select Case KeyAscii
Case vbKeyReturn
' When the user hits the return key
' this code'll move the next cell or row.
Call DataEntryCell
If booMsgBox = True Then
booMsgBox = False
End If
With ActiveGrid
If .col + 1 <= .Cols - 1 Then
.col = .col + 1
Active_FlxCell.col = .col
ElseIf .row + 1 <= .Rows - 1 Then
.row = .row + 1
Active_FlxCell.row = .row
'.col = 0
Else
.row = 1
.col = 0
End If
DataEntryBox ActiveGrid
End With
End Select
End Sub
Last edited by Stan; Dec 8th, 2004 at 09:33 AM.
-
Dec 8th, 2004, 08:09 AM
#2
Re: Using vbKeyReturn in KeyPress Event of a TextBox
If I remember correctly, changing the FORM's parameter for KEYPREVIEW to FALSE will stop that ding. I could be wrong...
-
Dec 8th, 2004, 08:23 AM
#3
Thread Starter
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
If I remember correctly, changing the FORM's parameter for KEYPREVIEW to FALSE will stop that ding. I could be wrong...
It is set to FALSE... so that's not the problem. Anyone have other suggestions?
-
Dec 8th, 2004, 08:35 AM
#4
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Here some Code I use to make sure it "N/Y"
Code:
Private Sub txtTempPostNY_KeyPress(KeyAscii As Integer)
If KeyAscii = 121 Then
KeyAscii = 89
ElseIf KeyAscii = 110 Then
KeyAscii = 78
ElseIf KeyAscii = 89 Or KeyAscii = 78 Or KeyAscii = 8 Then
KeyAscii = KeyAscii
Else
Beep
KeyAscii = 0
End If
End Sub
Mudfish AKA Bowfin
I can spell "If" all day right, just a coder!
"Always do sober what you said you'd do drunk. That will teach you to keep your mouth shut." -- Ernest Hemingway
Member of the ECCC

-
Dec 8th, 2004, 08:47 AM
#5
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Code:
Private Sub txtEdit_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Call DataEntryCell
If booMsgBox = True Then
booMsgBox = False
End If
With ActiveGrid
If .col + 1 <= .Cols - 1 Then
.col = .col + 1
Active_FlxCell.col = .col
ElseIf .row + 1 <= .Rows - 1 Then
.row = .row + 1
Active_FlxCell.row = .row
'.col = 0
Else
.row = 1
.col = 0
End If
DataEntryBox ActiveGrid
End With
End If
End Sub
So you would want this!
Last edited by mudfish; Dec 8th, 2004 at 08:50 AM.
Mudfish AKA Bowfin
I can spell "If" all day right, just a coder!
"Always do sober what you said you'd do drunk. That will teach you to keep your mouth shut." -- Ernest Hemingway
Member of the ECCC

-
Dec 8th, 2004, 08:52 AM
#6
Thread Starter
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Mudfish... I don't understand what you're doing...
Private Sub txtTempPostNY_KeyPress(KeyAscii As Integer)
If KeyAscii = 121 Then F10 key
KeyAscii = 89 Y key
ElseIf KeyAscii = 110 Then decimal point
KeyAscii = 78 N key
ElseIf KeyAscii = 89 Or KeyAscii = 78 Or KeyAscii = 8 Back space Then
KeyAscii = KeyAscii
Else
Beep
KeyAscii = 0
End If
End Sub
-
Dec 8th, 2004, 09:01 AM
#7
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
 Originally Posted by Stan
Mudfish... I don't understand what you're doing...
Private Sub txtTempPostNY_KeyPress(KeyAscii As Integer)
If KeyAscii = 121 Then y key
KeyAscii = 89 Y key
ElseIf KeyAscii = 110 Then n key
KeyAscii = 78 N key
ElseIf KeyAscii = 89 Or KeyAscii = 78 Or KeyAscii = 8 Back space Then
KeyAscii = KeyAscii
Else
Beep
KeyAscii = 0
End If
End Sub
That the small y and n! The back space allow them to do it over.
Mudfish AKA Bowfin
I can spell "If" all day right, just a coder!
"Always do sober what you said you'd do drunk. That will teach you to keep your mouth shut." -- Ernest Hemingway
Member of the ECCC

-
Dec 8th, 2004, 09:09 AM
#8
Banned
Re: Using vbKeyReturn in KeyPress Event of a TextBox
LOL
KeyAscii = KeyAscii
what is the use of that
-
Dec 8th, 2004, 09:12 AM
#9
Thread Starter
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Perhaps I'm slow, but what does any of these responses have to do with my problem?
-
Dec 8th, 2004, 09:13 AM
#10
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
I block with a beep every thing but back space, little y, big Y, little n and Big N.
If it is little n or y I upper case them.
Mudfish AKA Bowfin
I can spell "If" all day right, just a coder!
"Always do sober what you said you'd do drunk. That will teach you to keep your mouth shut." -- Ernest Hemingway
Member of the ECCC

-
Dec 8th, 2004, 09:15 AM
#11
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Stan the KeyAscii for the enter button is 13
Mudfish AKA Bowfin
I can spell "If" all day right, just a coder!
"Always do sober what you said you'd do drunk. That will teach you to keep your mouth shut." -- Ernest Hemingway
Member of the ECCC

-
Dec 8th, 2004, 09:22 AM
#12
Thread Starter
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
 Originally Posted by mudfish
Stan the KeyAscii for the enter button is 13
Mudfish... I'm aware of that. I appreciate your attention to my posting but it's not addressing my original problem (at least I don't see it does).
-
Dec 8th, 2004, 09:26 AM
#13
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
 Originally Posted by mudfish
Code:
Private Sub txtEdit_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Call DataEntryCell
If booMsgBox = True Then
booMsgBox = False
End If
With ActiveGrid
If .col + 1 <= .Cols - 1 Then
.col = .col + 1
Active_FlxCell.col = .col
ElseIf .row + 1 <= .Rows - 1 Then
.row = .row + 1
Active_FlxCell.row = .row
'.col = 0
Else
.row = 1
.col = 0
End If
DataEntryBox ActiveGrid
End With
End If
End Sub
So you would want this!
Try this. I do not think that "Case vbKeyReturn", is working the way you want it to or change Case vbKeyReturn to Case 13
Mudfish AKA Bowfin
I can spell "If" all day right, just a coder!
"Always do sober what you said you'd do drunk. That will teach you to keep your mouth shut." -- Ernest Hemingway
Member of the ECCC

-
Dec 8th, 2004, 09:29 AM
#14
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Before you call your function, set KEYASCII = 0 - you've already trapped the action and done your business - I think leaving the KEYPRESS event with KEYASCII having a 13 value could be the problem.
-
Dec 8th, 2004, 09:29 AM
#15
Thread Starter
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Mudfish... the problem doesn't seem to be related to the Case vbKeyReturn. I have the same problem with using
Code:
If KeyAscii = 13 Then
-
Dec 8th, 2004, 09:30 AM
#16
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
What line is the problem?
Mudfish AKA Bowfin
I can spell "If" all day right, just a coder!
"Always do sober what you said you'd do drunk. That will teach you to keep your mouth shut." -- Ernest Hemingway
Member of the ECCC

-
Dec 8th, 2004, 09:30 AM
#17
Banned
Re: Using vbKeyReturn in KeyPress Event of a TextBox
vbKeyReturn = 13
what is the difrence between 13 and 13?
-
Dec 8th, 2004, 09:34 AM
#18
Re: Using vbKeyReturn in KeyPress Event of a TextBox
MAn, I want to smash my head in from reading this....
Your error handler is in the wrong spot. It doesn't go at the top -- that will cause it to run every time....
Code:
Private Sub txtEdit_KeyPress(KeyAscii As Integer)
'Each keystroke generates an error - text is still put into textbox
On Error GoTo errhandler
Select Case KeyAscii
Case vbKeyReturn
' When the user hits the return key
' this code'll move the next cell or row.
Call DataEntryCell
If booMsgBox = True Then
booMsgBox = False
End If
With ActiveGrid
If .col + 1 <= .Cols - 1 Then
.col = .col + 1
Active_FlxCell.col = .col
ElseIf .row + 1 <= .Rows - 1 Then
.row = .row + 1
Active_FlxCell.row = .row
'.col = 0
Else
.row = 1
.col = 0
End If
DataEntryBox ActiveGrid
End With
End Select
Exit Sub
errhandler:
MsgBox Err.number & " - " & Err.Description
End Sub
That's why you got a Error number of 0. It means there is no error. But you had the message box there and so it would run. You need to put the error handler at the bottom, only go there when there's an error, and exit the sub before you hit the code when there is no error.
Tg
-
Dec 8th, 2004, 09:34 AM
#19
Thread Starter
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
 Originally Posted by szlamany
Before you call your function, set KEYASCII = 0 - you've already trapped the action and done your business - I think leaving the KEYPRESS event with KEYASCII having a 13 value could be the problem.
That fixed the problem... many thanks
-
Dec 8th, 2004, 09:38 AM
#20
Thread Starter
Hyperactive Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox
 Originally Posted by techgnome
MAn, I want to smash my head in from reading this....
Your error handler is in the wrong spot. It doesn't go at the top -- that will cause it to run every time....
Code:
Private Sub txtEdit_KeyPress(KeyAscii As Integer)
'Each keystroke generates an error - text is still put into textbox
On Error GoTo errhandler
Select Case KeyAscii
Case vbKeyReturn
' When the user hits the return key
' this code'll move the next cell or row.
Call DataEntryCell
If booMsgBox = True Then
booMsgBox = False
End If
With ActiveGrid
If .col + 1 <= .Cols - 1 Then
.col = .col + 1
Active_FlxCell.col = .col
ElseIf .row + 1 <= .Rows - 1 Then
.row = .row + 1
Active_FlxCell.row = .row
'.col = 0
Else
.row = 1
.col = 0
End If
DataEntryBox ActiveGrid
End With
End Select
Exit Sub
errhandler:
MsgBox Err.number & " - " & Err.Description
End Sub
Calm down.... I am aware of the proper "etiquette" for error handling. I wrote it the way I did because I wanted to see what was happening (also, did you not see that it was REM'd out - ie. not active).
-
Dec 8th, 2004, 09:42 AM
#21
Re: Using vbKeyReturn in KeyPress Event of a TextBox
 Originally Posted by Stan
That fixed the problem... many thanks 
You are very welcome - I don't think anyone understood your problem.
We've had it ourselves - I just wasn't at work when I gave my first reply...
-
Dec 8th, 2004, 09:49 AM
#22
Banned
Re: Using vbKeyReturn in KeyPress Event of a TextBox [RESOLVED]
i knew what he ment but i coulndt come from laughing because so many posts on such simple problem
-
Dec 11th, 2004, 08:20 PM
#23
New Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox [RESOLVED]
I have the same problem and I don't quite understand what you are saying. When I write something in my textbox and press enter it beeps. This is my code:
Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
RichTextBox1.Text = RichTextBox1.Text & Text2.Text & vbCrLf
Winsock1.SendData Text2.Text & vbCrLf
Text2.Text = ""
Text2.SetFocus
End If
End Sub
What should I do to make it stop beeping?
-
Dec 11th, 2004, 08:53 PM
#24
Re: Using vbKeyReturn in KeyPress Event of a TextBox [RESOLVED]
Set KeyAscii to the value 0 inside the IF/THEN
-
Dec 11th, 2004, 08:57 PM
#25
New Member
Re: Using vbKeyReturn in KeyPress Event of a TextBox [RESOLVED]
 Originally Posted by szlamany
Set KeyAscii to the value 0 inside the IF/THEN
Thanks!
-
Dec 11th, 2004, 11:48 PM
#26
Re: Using vbKeyReturn in KeyPress Event of a TextBox [RESOLVED]
I wrote about the exact same problem twice before, and until today, haven't been able to find out how to get rid of it. It is more of a DING rather than a BEEP. I had removed all beeps from the program, and still had the annoying DING!
Thanks, szlammy. That did the trick!
I fee'l like putting something in CODEBANK about 'Removing DING from Keypress event"
LOL !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|