Close two forms with one event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rdemyan via AccessMonster.com

    Close two forms with one event

    I need help with code to close two forms at the same time, FormA and FormB. I
    need code that can be used from either form and needs to work with both a
    'Close' button on each form as well as if the user clicks the 'X' in the
    control box.

    I have a function, IsObjectOpen, that can be used to check if a form is open.
    The function is used as follows:

    X = IsObjectOpen(Fo rmName,2)

    where 2 is for forms.

    I've tried to code this but I keep getting errors that an object is closed
    already when I try closing it. It seems like what is happening that code
    within FormA or FormB close event is trying to close the other form. But when
    that code executes it too tries to close the other form so a conflict occurs.
    I thought the IsObjectOpen form would take care of this, but it appears to be
    claiming a form is open when it is in the act of being closed, so the close
    event tries to execute once.

    IMPORTANT: The code needs to be able to work from either a close button on
    either form or when the user clicks on the 'X' in the control box in either
    form. I haven't been able to get this to work correctly for all 4 possible
    ways of closing both forms.

    Help on how to code this is greatly appreciated.

    Thank you.

    --
    Message posted via http://www.accessmonster.com
  • Terry Kreft

    #2
    Re: Close two forms with one event

    Essentially you close the other form in this forms unload event e.g.

    2 forms ( Form1 and Form2) each with a command button on them

    ' Code behind Form1
    Option Explicit

    Private Sub Command0_Click( )
    DoCmd.Close acForm, Me.Name, acSaveYes
    End Sub

    Private Sub Form_Unload(Can cel As Integer)
    On Error Resume Next
    DoCmd.Close acForm, "Form2", acSaveYes
    End Sub

    ' Code behind Form2
    Option Explicit

    Private Sub Command0_Click( )
    DoCmd.Close acForm, Me.Name, acSaveYes
    End Sub

    Private Sub Form_Unload(Can cel As Integer)
    On Error Resume Next
    DoCmd.Close acForm, "Form1", acSaveYes
    End Sub


    --

    Terry Kreft


    "rdemyan via AccessMonster.c om" <u6836@uwe> wrote in message
    news:5fb6e3f2da 5d2@uwe...[color=blue]
    > I need help with code to close two forms at the same time, FormA and[/color]
    FormB. I[color=blue]
    > need code that can be used from either form and needs to work with both a
    > 'Close' button on each form as well as if the user clicks the 'X' in the
    > control box.
    >
    > I have a function, IsObjectOpen, that can be used to check if a form is[/color]
    open.[color=blue]
    > The function is used as follows:
    >
    > X = IsObjectOpen(Fo rmName,2)
    >
    > where 2 is for forms.
    >
    > I've tried to code this but I keep getting errors that an object is closed
    > already when I try closing it. It seems like what is happening that code
    > within FormA or FormB close event is trying to close the other form. But[/color]
    when[color=blue]
    > that code executes it too tries to close the other form so a conflict[/color]
    occurs.[color=blue]
    > I thought the IsObjectOpen form would take care of this, but it appears to[/color]
    be[color=blue]
    > claiming a form is open when it is in the act of being closed, so the[/color]
    close[color=blue]
    > event tries to execute once.
    >
    > IMPORTANT: The code needs to be able to work from either a close button[/color]
    on[color=blue]
    > either form or when the user clicks on the 'X' in the control box in[/color]
    either[color=blue]
    > form. I haven't been able to get this to work correctly for all 4[/color]
    possible[color=blue]
    > ways of closing both forms.
    >
    > Help on how to code this is greatly appreciated.
    >
    > Thank you.
    >
    > --
    > Message posted via http://www.accessmonster.com[/color]


    Comment

    • Jim Andersen

      #3
      Re: Close two forms with one event

      rdemyan via AccessMonster.c om wrote:[color=blue]
      > I've tried to code this but I keep getting errors that an object is
      > closed already when I try closing it.[/color]

      Is that a problem ?

      /jim


      Comment

      • rdemyan via AccessMonster.com

        #4
        Re: Close two forms with one event

        This is essentially the same strategy I used except for two things:

        1) Instead of the Form Unload event I was using the Form Close event
        2) I was trapping any error as opposed to using Resume Next.

        It looks like the On Error Resume Next is the key.

        Thanks.

        Terry Kreft wrote:[color=blue]
        >Essentially you close the other form in this forms unload event e.g.
        >
        >2 forms ( Form1 and Form2) each with a command button on them
        >
        >' Code behind Form1
        >Option Explicit
        >
        >Private Sub Command0_Click( )
        > DoCmd.Close acForm, Me.Name, acSaveYes
        >End Sub
        >
        >Private Sub Form_Unload(Can cel As Integer)
        > On Error Resume Next
        > DoCmd.Close acForm, "Form2", acSaveYes
        >End Sub
        >
        >' Code behind Form2
        >Option Explicit
        >
        >Private Sub Command0_Click( )
        > DoCmd.Close acForm, Me.Name, acSaveYes
        >End Sub
        >
        >Private Sub Form_Unload(Can cel As Integer)
        > On Error Resume Next
        > DoCmd.Close acForm, "Form1", acSaveYes
        >End Sub
        >[color=green]
        >> I need help with code to close two forms at the same time, FormA and FormB. I
        >> need code that can be used from either form and needs to work with both a[/color]
        >[quoted text clipped - 24 lines][color=green]
        >>
        >> Thank you.[/color][/color]

        --
        Message posted via http://www.accessmonster.com

        Comment

        Working...