Solved

Unload Me

Posted on 2004-08-10
7
1,248 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]
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

813 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

18 Experts available now in Live!

Get 1:1 Help Now