-
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
-
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...
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Quote:
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?
-
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
-
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!
-
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
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Quote:
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.
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
LOL
KeyAscii = KeyAscii
what is the use of that
-
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?
-
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.
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Stan the KeyAscii for the enter button is 13
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Quote:
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). :)
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Quote:
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
-
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.
-
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
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
What line is the problem?
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
vbKeyReturn = 13
what is the difrence between 13 and 13?
-
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
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Quote:
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 :thumb:
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Quote:
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).
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox
Quote:
Originally Posted by Stan
That fixed the problem... many thanks :thumb:
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...
:)
-
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 :wave:
-
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?
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox [RESOLVED]
Set KeyAscii to the value 0 inside the IF/THEN
-
Re: Using vbKeyReturn in KeyPress Event of a TextBox [RESOLVED]
Quote:
Originally Posted by szlamany
Set KeyAscii to the value 0 inside the IF/THEN
Thanks!
-
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 !