Cancel = true

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,640
Office Version
  1. 365
Platform
  1. 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

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
 

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,012
Office Version
  1. 365
Platform
  1. Windows
Have you actually tried it?
 

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,640
Office Version
  1. 365
Platform
  1. Windows
Yes I did actually and it is working but to be honest, it does not make sense. That is why I wanted to confirm. Thank you
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,012
Office Version
  1. 365
Platform
  1. Windows
You're welcome
 

Watch MrExcel Video

Forum statistics

Threads
1,122,729
Messages
5,597,795
Members
414,176
Latest member
LK88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top