Solved

Unload Me

Posted on 2004-08-10
7
1,251 Views
Last Modified: 2012-06-27
Hi everyone, I'm trying to abort exiting the program if user choses no to exit on a msgbox. that sets the var AbortTask = true. this that I wrote doesn't work. I don't understang why...

Can somebody help me?

Private Sub Form_Unload(Cancel As Integer)
    If LoadingBar.Visible = True Then
        Cancel = 1
        Exit Sub
    Else: End If
   
    If AbortTask = True Then
        AbortTask = False
        Cancel = 1
        Exit Sub
    Else: End If
End Sub

The msgbox must be from another form or sub, so I have to work with a boolean var I guess.
0
Comment
Question by:igfp
[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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 11761626

Rather use the QueryUnload even for checking if the user really wants to close:

Private Sub Form_QueryUnload(unloadmode, cancel ... i don't know the arguments by heart)
    If LoadingBar.Visible = True Then
        Cancel = 1
        Exit Sub
    End If
   
    If AbortTask = True Then
        AbortTask = False
        Cancel = 1
        Exit Sub
    End If
End Sub


Alternative:

Private Sub Form_QueryUnload(unloadmode, cancel ... i don't know the arguments by heart)
    If LoadingBar.Visible = True Then
        Cancel = 1
        Exit Sub
    End If

    if unloadmode = vbFormControl then
       if msgbox ( 'do you really wanna quit?', vbyesno ) = vbno then
         cancel = true
       endif
    endif    

End Sub


CHeers
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 11761644
Where do you define AbortTask variable?

You should define in module as Public not on the form
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 11761653
Is the variable global?
When do you set it?
If the Unload procedure is in several Forms, the variable will be reset the first time that it is encountered.
I suggest stepping throught the code (set a breakpoint on the first line of the sub)
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:igfp
ID: 11761669
Yes the var is set on a module as this: Public AbortTask as boolean
When the program is load it is set as false.

I tried query unload too, and it didn't work. don't know how...
0
 

Author Comment

by:igfp
ID: 11761678
I also made the breakpoint and the form actually passes the line: Cancel=1 it stops there. but afterwards, the program exits!
0
 
LVL 15

Assisted Solution

by:SRigney
SRigney earned 200 total points
ID: 11761709
Cancel = 1 should prevent the form from unloading, but if you have an End statement the Unload event won't stop that.
0
 
LVL 19

Accepted Solution

by:
Shauli earned 200 total points
ID: 11761952
'The message box should be placed in the quesryUnload event, and anywhere you want
'to exit just put "Unload FormName" or "Unload Me". Never use End anywhere in your form except as the last line in an Unload event,
'and try this code:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If MsgBox("Do you wish to exit", vbQuestion + vbYesNo, "My Application Control") = vbNo Then
    Cancel = True
    Exit Sub
End If
End Sub

S
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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

732 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