473,508 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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
3 4380
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3073
by: Nick250 | last post by:
I have a simple app that I created for our Real Estate office using Access 2000 and it is working fine except for on thing. I need to keep the close button hidden from the users. No problem...
2
9000
by: Guy Babbitt | last post by:
I have an MDI application that starts an instance of a child form at application start. I have an event handler on a combo box checking for the selected value to change. When the select value...
2
1650
by: Sunil Sabir | last post by:
Dear All, I have a Main Asp.Net page . In that Page I have a Tab Strip which has SIX tabs each representing a different web Forms.So that when a user clicks different tab he can navigate between...
0
1422
by: Bruce | last post by:
I have an MDI parent form frmMain that *should* allow multiple instances of a child form frmChild. The frmChild operates completely independently of frmMain (aside from being an MDI child), and...
5
11054
by: Stan Sainte-Rose | last post by:
Hi, Which event is called when the user click on the close window icon (X) ? I want, when he clicks on this icon, to display a message before closing the form. If he replys by No, I don't want to...
10
10016
by: morangolds | last post by:
Hi, I've been having a problem with C++ Windows Forms apps not "ending" when you close the form window. I've searched about this problem all over the place and most searches have lead me to...
4
13076
by: PW | last post by:
Hi, I want to add code to check if the user wants to save the record (fields are unbound) when they press the form's close (X) button. Is it possible to return the user to the form if there...
8
3318
by: Rodrigo Juarez | last post by:
Hi I'm using Visual Studio 2005 with visual basic, developing winforms applications I'm adding try catch blocks for error handling and I want to close the application when I got an error....
0
1988
by: BillCo | last post by:
I'm a C# Newbie, so if I've missed the obvious here go easy!!! I have a form opened from a MDI parent. The form has a data bound combo box, with a SelectedIndexChanged event (fills a list view...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.