Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Userform Cancel

I am always amazed at how I can't find answers to the simplest
questions.

I am loading a userform. The form has the normal "close" box in the
upper right corner.

It appears that if that box is clicked, the form unloads. This can
cause some problems with my later program flow. I have an "exit" box
on the form itself, but that lets me run some code.

How do I capture the event where the user clicks the "x" to close the
form? I have events like workbook_beforeclose but I can't find a
similar event for a form. Alternatively, how do I eliminate the close
box from the form, so that the user has to click on "exit?"

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,117
Default Userform Cancel

Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use the button to close the form", vbInformation

End If
End Sub
xxxxxxxxxxxxxxxxx
i don't need this, usually, but somebody posted it once & i snatched it
"in case".
susan


Hank Youngerman wrote:
I am always amazed at how I can't find answers to the simplest
questions.

I am loading a userform. The form has the normal "close" box in the
upper right corner.

It appears that if that box is clicked, the form unloads. This can
cause some problems with my later program flow. I have an "exit" box
on the form itself, but that lets me run some code.

How do I capture the event where the user clicks the "x" to close the
form? I have events like workbook_beforeclose but I can't find a
similar event for a form. Alternatively, how do I eliminate the close
box from the form, so that the user has to click on "exit?"


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Userform Cancel

There are a number of ways the close can be invoked, all can be handled via
query close. This extract from help shows the values for CloseMode

vbFormControlMenu 0 The user has chosen the Close command from the
Control menu on the UserForm.
vbFormCode 1 The Unload statement is invoked from code.
vbAppWindows 2 The current Windows operating environment session is
ending.
vbAppTaskManager 3 The Windows Task Manager is closing the
application.



--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Susan" wrote in message
ups.com...
Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use the button to close the form", vbInformation

End If
End Sub
xxxxxxxxxxxxxxxxx
i don't need this, usually, but somebody posted it once & i snatched it
"in case".
susan


Hank Youngerman wrote:
I am always amazed at how I can't find answers to the simplest
questions.

I am loading a userform. The form has the normal "close" box in the
upper right corner.

It appears that if that box is clicked, the form unloads. This can
cause some problems with my later program flow. I have an "exit" box
on the form itself, but that lets me run some code.

How do I capture the event where the user clicks the "x" to close the
form? I have events like workbook_beforeclose but I can't find a
similar event for a form. Alternatively, how do I eliminate the close
box from the form, so that the user has to click on "exit?"




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Userform Cancel

Answer from Walkenbach's "Excel 2003 Power Programming with VBA" (whom I have
no connection with, except that I learned almost everything I know from his
books):

You can't disable the Close button, but you can write a procedure for the
"QueryClose" event, which is triggered by a command to close the UserForm and
executes BEFORE the form actually closes. The UserForm_QueryClose procedure
has 2 arguments. The CloseMode argument contains a value that indicates the
cause of QueryClose being triggered. When CloseMode=vbFormControlMenu, that
means the user clicked the "X" (close) button. Just write an If statement to
test CloseMode for vbFormControlMenu, and if true set the second argument --
Cancel -- to True, which cancels the close command.

If you have any confusion with this, email me and I'll send you the whole
section from Walkenbach's book. (I've never used this particular method, so
I can't personally vouch for it.)

"Hank Youngerman" wrote:

I am always amazed at how I can't find answers to the simplest
questions.

I am loading a userform. The form has the normal "close" box in the
upper right corner.

It appears that if that box is clicked, the form unloads. This can
cause some problems with my later program flow. I have an "exit" box
on the form itself, but that lets me run some code.

How do I capture the event where the user clicks the "x" to close the
form? I have events like workbook_beforeclose but I can't find a
similar event for a form. Alternatively, how do I eliminate the close
box from the form, so that the user has to click on "exit?"


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get userform Cancel button to invoke Exit Sub in calling macro? Craig Remillard New Users to Excel 1 November 19th 09 03:55 AM
userform cancel button davegb Excel Programming 3 June 12th 06 05:23 PM
Userform SetFocus with Exit and Cancel Neal Excel Programming 3 January 31st 06 01:21 AM
Cancel a Procedure through a Userform jumpjump[_3_] Excel Programming 1 August 30th 05 09:52 AM
Cancel Button on Userform KJ Dahl Excel Programming 2 December 19th 04 07:47 PM


All times are GMT +1. The time now is 07:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"