Results 1 to 3 of 3
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cancel cancelled? (VBA/ Excel 2000)

    This seems like a strange problem, which I may have always had or it may have just developed. I would usually place an End command in the click event of a CommandButton I nominated as the cancel button on a user form. This time I forgot and decided to simply set its Cancel property to True instead. This doesn't seem to work as I understand it should. Is that all that's required, or does one still need to write the event procedure to close the form? The Default property works OK - it will run the appropriate code for the default button click - but the Cancel property seems to have no effect. Am I missing something?

    Alan

    Edited - Trying to read behind the lines in my help file, I tend to think that I do need to supply the code, and that all the Cancel property does is to direct the Esc key to that code. But I'll leave this up anyway, because I'm still unsure.

  2. #2
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cancel cancelled? (VBA/ Excel 2000)

    Thanks Hans. Perfectly clear as usual. And yes, I used End only as a (bad) example. I'd usually use Hide (if moving to another related form in a series, say) and Unload in the manner you describe. That said, I didn't realize that End was as "drastic" as you point out.

    cheers

    Alan

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Cancel cancelled? (VBA/ Excel 2000)

    Setting the Cancel property for a command button to True only means that pressing the Esc key is the same as clicking the button. You still need code in the On Click event procedure to close the form or whatever.

    Similarly, setting the Default property of a command button to True only means that pressing the Enter key is the same as clicking the button.

    End is a very drastic way to end code execution. This is from the online help:
    <hr>Remarks

    When executed, the End statement resets all module-level variables and all static local variables in all modules. To preserve the value of these variables, use the Stop statement instead. You can then resume execution while preserving the value of those variables.

    Note: The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events of forms and class modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.

    The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing.<hr>
    The 'standard' code to close a userform is either Unload Me, which closes the form and releases the memory associated with it, or Me.Hide, which only makes it invisible, but still available in code (so you can inspect what the user entered in text boxes etc.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •