Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Userform Queryclose and Unload problem

Hello!

I have a user form with a single Cancel button on it. The code behind the
form is as follows:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
''' Redirect all non-code form closes through the Cancel button procedure.
If CloseMode < vbFormCode Then
Cancel = True
Command_Cancel_Click
End If
End Sub

Private Sub Command_Cancel_Click()
gbCancel = True 'global flag
Me.Hide
Unload Me
End Sub

Private Sub UserForm_Initialize()
MsgBox "Initialize"
End Sub

Private Sub UserForm_Terminate()
MsgBox "Terminate"
End Sub

If the user clicks the Cancel button, the Command_Cancel sub runs and the
userform is unloaded and the UserForm_Terminate event fires. Next time the
form is displayed with:
Load Userform1
Userform1.Show
then the UserForm_Initialize event fires as expected.

If the user closes the form with the close button (top right cross), the
Userform_QueryClose event captures this and redirects to the
Command_Cancel_Click sub. This all works fine, however the userform never
gets unloaded this way- even though the Unload Me line gets run, the
_Terminate event doesn't run, and next time there is a
Load Userform1
Userform1.Show
then the _Initialise event does not run as the form is already loaded.

Can anyone tell me why there is this difference in behaviour between closing
with the Cancel button, and closing with form's close button?

Thanks,
Dave
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default Userform Queryclose and Unload problem

Dave,

You don't need this line:

Cancel = True

Because you set Cancel=True the form doesn't unload when you close it with
the cross button.



"Dave Ramage" wrote:

Hello!

I have a user form with a single Cancel button on it. The code behind the
form is as follows:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
''' Redirect all non-code form closes through the Cancel button procedure.
If CloseMode < vbFormCode Then
Cancel = True
Command_Cancel_Click
End If
End Sub

Private Sub Command_Cancel_Click()
gbCancel = True 'global flag
Me.Hide
Unload Me
End Sub

Private Sub UserForm_Initialize()
MsgBox "Initialize"
End Sub

Private Sub UserForm_Terminate()
MsgBox "Terminate"
End Sub

If the user clicks the Cancel button, the Command_Cancel sub runs and the
userform is unloaded and the UserForm_Terminate event fires. Next time the
form is displayed with:
Load Userform1
Userform1.Show
then the UserForm_Initialize event fires as expected.

If the user closes the form with the close button (top right cross), the
Userform_QueryClose event captures this and redirects to the
Command_Cancel_Click sub. This all works fine, however the userform never
gets unloaded this way- even though the Unload Me line gets run, the
_Terminate event doesn't run, and next time there is a
Load Userform1
Userform1.Show
then the _Initialise event does not run as the form is already loaded.

Can anyone tell me why there is this difference in behaviour between closing
with the Cancel button, and closing with form's close button?

Thanks,
Dave

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Userform Queryclose and Unload problem

Thanks Vergel, removing this solves the problem.

"Vergel Adriano" wrote:

Dave,

You don't need this line:

Cancel = True

Because you set Cancel=True the form doesn't unload when you close it with
the cross button.



"Dave Ramage" wrote:

Hello!

I have a user form with a single Cancel button on it. The code behind the
form is as follows:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
''' Redirect all non-code form closes through the Cancel button procedure.
If CloseMode < vbFormCode Then
Cancel = True
Command_Cancel_Click
End If
End Sub

Private Sub Command_Cancel_Click()
gbCancel = True 'global flag
Me.Hide
Unload Me
End Sub

Private Sub UserForm_Initialize()
MsgBox "Initialize"
End Sub

Private Sub UserForm_Terminate()
MsgBox "Terminate"
End Sub

If the user clicks the Cancel button, the Command_Cancel sub runs and the
userform is unloaded and the UserForm_Terminate event fires. Next time the
form is displayed with:
Load Userform1
Userform1.Show
then the UserForm_Initialize event fires as expected.

If the user closes the form with the close button (top right cross), the
Userform_QueryClose event captures this and redirects to the
Command_Cancel_Click sub. This all works fine, however the userform never
gets unloaded this way- even though the Unload Me line gets run, the
_Terminate event doesn't run, and next time there is a
Load Userform1
Userform1.Show
then the _Initialise event does not run as the form is already loaded.

Can anyone tell me why there is this difference in behaviour between closing
with the Cancel button, and closing with form's close button?

Thanks,
Dave

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Userform Queryclose and Unload problem

Dave -

Not sure if this is it, but I use this IF in the QueryClose procedu

If CloseMode = vbFormControlMenu Then

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"Dave Ramage" wrote in message
...
Hello!

I have a user form with a single Cancel button on it. The code behind the
form is as follows:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
''' Redirect all non-code form closes through the Cancel button
procedure.
If CloseMode < vbFormCode Then
Cancel = True
Command_Cancel_Click
End If
End Sub

Private Sub Command_Cancel_Click()
gbCancel = True 'global flag
Me.Hide
Unload Me
End Sub

Private Sub UserForm_Initialize()
MsgBox "Initialize"
End Sub

Private Sub UserForm_Terminate()
MsgBox "Terminate"
End Sub

If the user clicks the Cancel button, the Command_Cancel sub runs and the
userform is unloaded and the UserForm_Terminate event fires. Next time the
form is displayed with:
Load Userform1
Userform1.Show
then the UserForm_Initialize event fires as expected.

If the user closes the form with the close button (top right cross), the
Userform_QueryClose event captures this and redirects to the
Command_Cancel_Click sub. This all works fine, however the userform never
gets unloaded this way- even though the Unload Me line gets run, the
_Terminate event doesn't run, and next time there is a
Load Userform1
Userform1.Show
then the _Initialise event does not run as the form is already loaded.

Can anyone tell me why there is this difference in behaviour between
closing
with the Cancel button, and closing with form's close button?

Thanks,
Dave



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
Show/Unload Userform StephanieH Excel Programming 2 August 24th 06 08:08 PM
unload userform problem leonidas[_35_] Excel Programming 3 July 5th 06 05:35 PM
Unload Userform doesn't work here - why? rammieib Excel Programming 1 February 23rd 06 01:51 PM
load/unload userform Fred[_22_] Excel Programming 3 August 22nd 05 04:00 AM
unload userform from within one of that form's beforeupdate events? Andrew H[_3_] Excel Programming 1 August 10th 04 09:07 AM


All times are GMT +1. The time now is 06:25 PM.

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"