|
-
Oct 20th, 2007, 01:48 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Unload problem
Hi people, 
I have trawled this forums topics and put together this which I hoped would become a 'one size fits all' piece of code. Problem is....when I cancel the unload from the 'X' button it works ok, but when I cancel the unload from the exit cmdbutton or menuexit it still unloads. Would you peeps be kind enough to have a look and give me some pointers?
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
On Error GoTo errorhandler
Dim frm As Form
Dim intPress As Integer
Select Case UnloadMode
Case vbFormControlMenu 'UnloadMode 0 'form is being unloaded via the Close or by hitting the X in the upper right hand corner command from the System menu
intPress = MsgBox("Are you sure you want to exit ? ", vbQuestion + vbOKCancel, "Exit Program")
If intPress <> vbOK Then
Cancel = -1
Exit Sub
Else
For Each frm In Forms
Unload frm
Next
End If
Case vbFormCode 'UnloadMode 1 - Unload Me has been issued from code
intPress = MsgBox("Are you sure you want to exit ? ", vbQuestion + vbOKCancel, "Exit Program")
If intPress = 2 Then
Cancel = -1
Exit Sub
Else
For Each frm In Forms
Unload frm
Next
End If
Case vbAppWindows 'UnloadMode 2
For Each frm In Forms
Unload frm
Next
Case vbAppTaskManager 'UnloadMode 3
For Each frm In Forms
Unload frm
Next
End Select
Exit Sub
errorhandler:
Number = Err.Number
Call Callerror(Number)
End Sub
Thankyou in anticipation.
regards
GB B)
-
Oct 20th, 2007, 01:52 PM
#2
Re: Unload problem
Change
If intPress = 2 Then
to
If intPress <> vbOK Then
-
Oct 20th, 2007, 02:50 PM
#3
Thread Starter
Addicted Member
Re: Unload problem
Mmm... no I tried that earlier, but I also tried just now to make sure.
Although it works ok for the 'vbFormControlMenu' section.
Any more thoughts...
-
Oct 20th, 2007, 03:35 PM
#4
Re: Unload problem
Well, change that to vbOK and also change your declaration to
Dim intpress As VbMsgBoxResult
-
Oct 20th, 2007, 03:47 PM
#5
Thread Starter
Addicted Member
Re: Unload problem
'Fraid not, it's still doing the same thing.
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
On Error GoTo errorhandler
Dim frm As Form
Dim intPress As VbMsgBoxResult
Select Case UnloadMode
Case vbFormControlMenu 'UnloadMode 0 'form is being unloaded via the Close or by hitting the X in the upper right hand corner command from the System menu
intPress = MsgBox("Are you sure you want to exit ? ", vbQuestion + vbOKCancel, "Exit Program")
If intPress <> vbOK Then
Cancel = -1
Exit Sub
Else
For Each frm In Forms
Unload frm
Next
End If
Case vbFormCode 'UnloadMode 1 - Unload Me has been issued from code
intPress = MsgBox("Are you sure you want to exit ? ", vbQuestion + vbOKCancel, "Exit Program")
If intPress <> vbOK Then
Cancel = -1
Exit Sub
Else
For Each frm In Forms
Unload frm
Next
End If
Case vbAppWindows 'UnloadMode 2
For Each frm In Forms
Unload frm
Next
Case vbAppTaskManager 'UnloadMode 3
For Each frm In Forms
Unload frm
Next
End Select
Exit Sub
errorhandler:
Number = Err.Number
Call Callerror(Number)
End Sub
-
Oct 20th, 2007, 03:54 PM
#6
Re: Unload problem
You didn't show us the code behind your exit command button.
-
Oct 20th, 2007, 04:01 PM
#7
Re: Unload problem
Well, pasting your code in to a new project, all works as you have it coded. Recommend placing a stop buton on If intPress = 2. And step thru it at that point. You might find that the code is exiting sub there and unloading somewhere else, somehow.
Here's another suggestion. Put your form unload loop in the Form_Unload event. It doesn't need to be in the Query_Unload because if you cancel out successfully, the Unload event doesn't fire anyway.
Your command button and menu item that calls unload, are they in the same form? Is this form's Unload only called from this form or is called from other forms?
-
Oct 20th, 2007, 04:01 PM
#8
Thread Starter
Addicted Member
Re: Unload problem
OH, my gawd, (MartinLiss) that was it...... for some reason there was an 'End' in there. Can't imagine why I never use the word, especially after all I've read in these forums.
Here's typically the code from menu & command button
Code:
Private Sub menuexit_Click()
Unload Form1
End '<--Offending (and embarassing) code which has been removed
End Sub
Well thanks a million guys, I appreciate your timely responses.
VBForums to the rescue again 
All the best
GB
Last edited by GettinBetter; Oct 20th, 2007 at 04:09 PM.
-
Oct 20th, 2007, 04:24 PM
#9
Re: [RESOLVED] Unload problem
Glad I culd help. One suggestion I could make would be to change
Code:
intPress = MsgBox("Are you sure you want to exit ? ", vbQuestion + vbOKCancel, "Exit Program")
'to
intPress = MsgBox("Are you sure you want to exit ? ", vbQuestion + vbYesNo, "Exit Program") ' or vbYesNoCancel
-
Oct 20th, 2007, 04:27 PM
#10
Re: [RESOLVED] Unload problem
Well two suggestions Change
Code:
intPress = MsgBox("Are you sure you want to exit ? ", vbQuestion + vbOKCancel, "Exit Program")
If intPress <> vbOK Then
'to
If MsgBox("Are you sure you want to exit ? ", vbQuestion + vbOKCancel, "Exit Program") <> vbOK Then
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|