Solved

Unload Me

Posted on 2004-08-10
7
1,245 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
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility

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
Comment Utility
Where do you define AbortTask variable?

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

Expert Comment

by:GrahamSkan
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:igfp
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
'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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now