lezawang
Well-known Member
- Joined
- Mar 27, 2016
- Messages
- 1,805
- Office Version
- 2016
- Platform
- Windows
Hi I have a form which has 2 text boxes, one for userName and one for password and 2 buttons, one Enter and one Cancel. The article below is saying I need to make Cancel properties of a command button to True then I can use ESC key to cancel. I want to understand that better. So here is my understanding:
I have to have Sub cancel_click() macro which will unload the form but instead of clicking on Cancel button, I can press ESC key to cancel, That is all! I mean if I change the Cancel property to TRUE and if I do not have Sub cancel_Click() macro, then it wont work. Am I right. Thank you very much
[h=2]Using the Escape key to cancel[/h]If you want to allow the user to cancel using the Esc it is simple(but not obvious) to do. You set the Cancel property of your ‘Cancel’ button to True. When Esc is pressed the click event of your Cancel button will be used.
https://excelmacromastery.com/vba-user-forms-1/#Using_the_Escape_key_to_cancel
I have to have Sub cancel_click() macro which will unload the form but instead of clicking on Cancel button, I can press ESC key to cancel, That is all! I mean if I change the Cancel property to TRUE and if I do not have Sub cancel_Click() macro, then it wont work. Am I right. Thank you very much
Code:
Private Sub cancel_Click()
Unload pwd
End Sub
Private Sub enter_Click()
If (username.Value <> "" And password.Value <> "") Then
MsgBox "logon successful"
Else
MsgBox "logon fail"
End If
End Sub
[h=2]Using the Escape key to cancel[/h]If you want to allow the user to cancel using the Esc it is simple(but not obvious) to do. You set the Cancel property of your ‘Cancel’ button to True. When Esc is pressed the click event of your Cancel button will be used.
https://excelmacromastery.com/vba-user-forms-1/#Using_the_Escape_key_to_cancel