Solved

simple unload cancel

Posted on 2001-08-10
7
469 Views
Last Modified: 2008-01-09
How do I cancel a form from unloading?

I tried query_unload and set cancel to true...and false..but that didnt seem to work.

pls help :)



saki
0
Comment
Question by:sakibomber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 12

Accepted Solution

by:
Preece earned 40 total points
ID: 6374318
Saki, set Cancel = True in the form_unload event!

Preece
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6374353
This event depends upon type of unloadmode. I mean, who calls the unload event, windows, cotrol box, and so on.
You must check this before set cancel to true or false.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6374358
From MSDN:
Tip 187: Determining Whether an Application Was Closed from the Control Menu
December 5, 1995

Abstract
This article explains how to prevent the user of your Microsoft? Visual Basic? application from unintentionally quitting the application.

The Query_Unload Event
When running an application, you can select the Close command from the Control-menu box in the upper-left corner of a window to quit the application. When running a Microsoft? Windows? 95?based application, you can click the Close button located at the right end of the title bar. These actions terminate the application immediately.

In a Microsoft Visual Basic? application, the Query_Unload event is triggered when the user tries to quit an application that is running. The UnloadMode variable in the Query_Unload event indicates how this event was triggered by containing one of the five values in the following table.

Value Description
0 The user chose the Close command on the Control-menu box.
1 The application used the Query_Unload method itself.
2 The operating system is being shut down.
3 The application is being shut down by the Task Manager.
4 An MDI form, which closes all child forms belonging to it, is being closed.


For example, when the user chooses the Close command from the Control-menu box to quit an application, the UnloadMode variable contains a value of zero.

Example Program
This program shows how to prevent a user from accidentally choosing the Close command on the program's Control-menu box.

Create a new project in Visual Basic. Form1 is created by default.


Add the following code to the Query_Unload event for Form1:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Dim I As Integer
    If (UnloadMode = FORM_CONTROLMENU) Then
        I = MsgBox("Exit " & App.Title & " ?", 4, App.EXEName)
        If I = 7 Then
            Cancel = True
            MsgBox "Program was not terminated"
        End If
        If I = 6 Then
            Cancel = False
            MsgBox "Program was terminated"
        End If
    End If
End Sub

Run the example program by pressing F5. Form1 appears on the screen. Either click the File menu and click the Close command or click the Close button in the upper right-hand corner of the form. The program displays a message box asking whether you really want to quit the program. If you click the Yes button, the program is immediately terminated. If you click the No button, however, the program continues to run.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sakibomber
ID: 6375678
If a menuitem is selected on a separate form, it closes my form and loads another.

'MenuItem form
Private Sub cmdMenuItem2_Click()
   If frmSettings.visible then
        frmSettings unload
   end if
End Sub


'In frmSettings
Private Sub Query_Unload(Cancel as Integer, UnloadMode as Integer)

If bMyDataHasChanged then
    Cancel = True
End If


End Sub

Is this what you're getting at???  Do you see anything wrong with this? I thought I tried this....but it still unloaded the form.

thanks again,


saki
0
 
LVL 6

Expert Comment

by:VK
ID: 6380545
Interesting, setting cancel = true should work but remember:

Perhaps the End - Command was executed before.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6381077
Put a breakpoint at queryunload of frmSettings and check the value of bMyDataHasChanged.
0
 

Author Comment

by:sakibomber
ID: 6441426
Sorry for the delay......the code was messier than anticipated.


thanks everyone!



saki
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question