Results 1 to 10 of 10

Thread: [RESOLVED] Unload problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    New Romney, Kent, UK
    Posts
    232

    Resolved [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)

  2. #2
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Unload problem

    Change

    If intPress = 2 Then

    to

    If intPress <> vbOK Then

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    New Romney, Kent, UK
    Posts
    232

    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...

  4. #4
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Unload problem

    Well, change that to vbOK and also change your declaration to

    Dim intpress As VbMsgBoxResult

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    New Romney, Kent, UK
    Posts
    232

    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

  6. #6

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    New Romney, Kent, UK
    Posts
    232

    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.

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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
  •  



Click Here to Expand Forum to Full Width