How To Disable the ENTER Key BEEP in a VB Text Box

This article was previously published under Q140882
This article has been archived. It is offered "as is" and will no longer be updated.
In a Microsoft Visual Basic for Windows text box, the ENTER key causes a warning beep to sound only if the MultiLine property is set to False (the default) and the Warning Beep option is selected in the Sound dialog box of the Windows Control panel.

To disable the beep, in the KeyPress event procedure for the text box, setthe value of KeyAscii (which is a parameter passed to KeyPress) equal tozero (0) when the user presses the ENTER key.
Specifically, use an IF statement to trap the ENTER key and the setKeyAscii to zero (0). Setting the value to zero before the event procedureends prevents Windows from detecting that the ENTER key was pressed and prevents the warning beep. This behavior is by design and is due to thefact that a non-multiline text box is a Windows default class of edit box.


The following code will prevent the beep:
   ' (Set Multiline property to False).   Private Sub Text1_KeyPress (KeyAscii as Integer)      If KeyAscii=13 Then         KeyAscii=0      End If   End Sub				

Article ID: 140882 - Last Review: 12/04/2015 12:33:50 - Revision: 2.2

Microsoft Visual Basic 4.0 Professional Edition, Microsoft Visual Basic 4.0 Professional Edition, Microsoft Visual Basic for Applications 5.0, Microsoft Visual Basic for Applications 6.0

  • kbnosurvey kbarchive kbhowto KB140882