Results 1 to 26 of 26

Thread: Using vbKeyReturn in KeyPress Event of a TextBox [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400

    Resolved 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.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400

    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?

  4. #4
    Hyperactive Member mudfish's Avatar
    Join Date
    Feb 2004
    Location
    Chit Chat
    Posts
    353

    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

  5. #5
    Hyperactive Member mudfish's Avatar
    Join Date
    Feb 2004
    Location
    Chit Chat
    Posts
    353

    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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400

    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

  7. #7
    Hyperactive Member mudfish's Avatar
    Join Date
    Feb 2004
    Location
    Chit Chat
    Posts
    353

    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.
    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

  8. #8

    Re: Using vbKeyReturn in KeyPress Event of a TextBox

    LOL

    KeyAscii = KeyAscii
    what is the use of that

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400

    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?

  10. #10
    Hyperactive Member mudfish's Avatar
    Join Date
    Feb 2004
    Location
    Chit Chat
    Posts
    353

    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

  11. #11
    Hyperactive Member mudfish's Avatar
    Join Date
    Feb 2004
    Location
    Chit Chat
    Posts
    353

    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

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400

    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).

  13. #13
    Hyperactive Member mudfish's Avatar
    Join Date
    Feb 2004
    Location
    Chit Chat
    Posts
    353

    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
    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

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400

    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

  16. #16
    Hyperactive Member mudfish's Avatar
    Join Date
    Feb 2004
    Location
    Chit Chat
    Posts
    353

    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

  17. #17

    Re: Using vbKeyReturn in KeyPress Event of a TextBox

    vbKeyReturn = 13

    what is the difrence between 13 and 13?

  18. #18
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400

    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

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400

    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).

  21. #21
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Using vbKeyReturn in KeyPress Event of a TextBox

    Quote 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...


  22. #22

    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

  23. #23
    New Member
    Join Date
    Dec 2004
    Posts
    2

    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?

  24. #24
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Using vbKeyReturn in KeyPress Event of a TextBox [RESOLVED]

    Set KeyAscii to the value 0 inside the IF/THEN

  25. #25
    New Member
    Join Date
    Dec 2004
    Posts
    2

    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!

  26. #26
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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
  •  



Click Here to Expand Forum to Full Width