Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Posts
    46

    Unanswered: Close all open forms?

    I am attempting to close all open forms in my application when the user exits the main application form. When the Unload code executes, I get the error "Run-time error 2501: The close action was cancelled"

    It is happening on the Docmd.Close statement of my code.

    I know there is probably a simple solution, but I've exhausted all of my ideas (which weren't that many).

    Thanks,
    Brian

    Here is my code:

    Private Sub Form_Unload(Cancel As Integer)
    response = MsgBox("Are you sure you want to exit out of the application?", vbQuestion + vbYesNo + vbCritical)
    If response = vbYes Then
    Do While Forms.Count > 0
    DoCmd.Close acForm, Forms(0).Name
    Loop
    Else
    Cancel = True
    End If
    End Sub

  2. #2
    Join Date
    Sep 2002
    Location
    Land of OZ
    Posts
    173

    Re: Close all open forms?

    Tru this out for size

    Private Sub Form_Close()
    response = MsgBox("Are you sure you want to exit out of the application?", vbQuestion + vbYesNo + vbCritical)
    If response = vbYes Then
    Do While Forms.Count > 0
    If Forms(0).Name <> Me.Name Then
    DoCmd.Close acForm, Forms(0).Name
    Else
    Exit Sub
    End If
    Loop
    Else
    Cancel = True
    End If

    End Sub


    the reason you are getting the error is because you are calling a close on a form already closing. And I also added the code to the close event of the main form.

    Hope this works

    Originally posted by bmartin
    I am attempting to close all open forms in my application when the user exits the main application form. When the Unload code executes, I get the error "Run-time error 2501: The close action was cancelled"

    It is happening on the Docmd.Close statement of my code.

    I know there is probably a simple solution, but I've exhausted all of my ideas (which weren't that many).

    Thanks,
    Brian

    Here is my code:

    Private Sub Form_Unload(Cancel As Integer)
    response = MsgBox("Are you sure you want to exit out of the application?", vbQuestion + vbYesNo + vbCritical)
    If response = vbYes Then
    Do While Forms.Count > 0
    DoCmd.Close acForm, Forms(0).Name
    Loop
    Else
    Cancel = True
    End If
    End Sub
    Mona
    ________________________________
    Life is too short to be sane or sensible. Weird people rule and normal people suck

  3. #3
    Join Date
    May 2002
    Posts
    46
    Hey Mona - thanks for your reply. I tweaked the code just a little to get it to work just right - Here's the finished sub:

    Private Sub Form_Unload(Cancel As Integer)
    response = MsgBox("Are you sure you want to exit out of the application?", vbQuestion + vbYesNo + vbCritical)

    Dim intx As Integer
    Dim intCount As Integer
    intCount = Forms.Count - 1
    For intx = intCount To 0 Step -1
    If Forms(intx).Name <> Me.Name Then
    DoCmd.Close acForm, Forms(intx).Name
    End If
    Next

    End Sub

    Thanks again!!
    Brian

  4. #4
    Join Date
    Sep 2002
    Location
    Land of OZ
    Posts
    173
    You're welcome ...

    Originally posted by bmartin
    Hey Mona - thanks for your reply. I tweaked the code just a little to get it to work just right - Here's the finished sub:

    Private Sub Form_Unload(Cancel As Integer)
    response = MsgBox("Are you sure you want to exit out of the application?", vbQuestion + vbYesNo + vbCritical)

    Dim intx As Integer
    Dim intCount As Integer
    intCount = Forms.Count - 1
    For intx = intCount To 0 Step -1
    If Forms(intx).Name <> Me.Name Then
    DoCmd.Close acForm, Forms(intx).Name
    End If
    Next

    End Sub

    Thanks again!!
    Brian
    Mona
    ________________________________
    Life is too short to be sane or sensible. Weird people rule and normal people suck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •