By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,495 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,495 IT Pros & Developers. It's quick & easy.

Close two forms with one event

P: n/a
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(FormName,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
May 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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(Cancel 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(Cancel As Integer)
On Error Resume Next
DoCmd.Close acForm, "Form1", acSaveYes
End Sub
--

Terry Kreft
"rdemyan via AccessMonster.com" <u6836@uwe> wrote in message
news:5fb6e3f2da5d2@uwe...
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(FormName,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

May 3 '06 #2

P: n/a
rdemyan via AccessMonster.com wrote:
I've tried to code this but I keep getting errors that an object is
closed already when I try closing it.


Is that a problem ?

/jim
May 4 '06 #3

P: n/a
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:
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(Cancel 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(Cancel As Integer)
On Error Resume Next
DoCmd.Close acForm, "Form1", acSaveYes
End Sub
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

[quoted text clipped - 24 lines]

Thank you.


--
Message posted via http://www.accessmonster.com
May 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.