Avoid beeps on forms without a default button

Avoid beeps on forms without a default button

If a form contains one CommandButton control whose Default property is set to True, the Enter key activates the CommandButton. If the form doesn’t contain any default CommandButton control, however, pressing the Enter key when the focus is inside a TextBox control (and a few other controls as well) results in an annoying beep.

You can get rid of this beep by setting the form’s KeyPreview property to True and adding the following code routine:

Private Sub Form_KeyPress(KeyAscii As Integer)    ' check the type of the control that is currently active    If TypeOf ActiveControl Is TextBox Then        ' discard the Enter key        If KeyAscii = vbKeyReturn Then KeyAscii = 0    End IfEnd Sub


Share the Post: