Disable that Beep while pressing the “Enter/Return” key in a Textbox using VB6

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
 

This works with Visual Basic 6.0 and VB 5.0.

'VB 5.0/6.0

'Copy and Paste this code in your Textbox_KeyPress() event.

    If KeyAscii = 13 Then  

        KeyAscii = 0   

    End If

7 thoughts on “Disable that Beep while pressing the “Enter/Return” key in a Textbox using VB6

  1. Mallikarjun

    I need a code which can transfer the data from one database to another with the help of Vb6.0 front end.
    Please provide me!

    Reply
  2. eric zant

    the beeping sound is caused by an error. if you output the error in a message box, it displays nothing. so the best way to suppress the beeping sound is to ‘resume next’ when an error is encountered. good luck

    Reply
  3. Dherman

    Man, I searched everywhere for this. People had things like inheriting special-case TextBoxes, setting weird properties to true, and override parent form keyhandling.

    But this was simple, and the solution that worked like a charm. Thanks!

    Reply
  4. Matthias

    The beep is an errormessage, if the key was not handled correctly, so you just need to set the key as correctly handled:
    e.g.

    sub anyname Handles YOURCONTROL.KeyPress

    ‘If enterkey
    If Asc(e.KeyChar) = Keys.Enter Then
    e.Handled = True
    ‘do something
    end if

    end sub

    Greetings

    Reply
  5. Claude

    Matthias is right.
    That’s one of the correct versions.
    I had an issue since I wasn’t placing the commands before the handled = true statement.
    His version is one of the correct ways to do this.
    Supposing you have declared e as the eventargs in the sub title
    Here’s a similiar code.
    Private Sub textbox_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles textbox.KeyPress

    Dim KeyAscii As Short = CShort(Asc(eventArgs.KeyChar))
    ‘On Error Resume Next
    If KeyAscii = System.Windows.Forms.Keys.Return Then
    eventArgs.Handled = True
    ‘code here
    end sub

    Reply
  6. Jason Post author

    The source code in this post is for VB 6.0.

    The source code you and Matthias posted won’t work for VB 6. Only for VB.NET. If you check my VB.NET post you will see code very similar to Matthias.

    Jason

    Reply

Leave a Reply

Your email address will not be published.