473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Close Current Form, Open a New One and vice-versa

Microblitz
Two weeks (Well about 8 hours actually) into writing VBA code and I've run up against a problem.

This is the senario;

----
I have a "Main" form in which an entry on a drop down selects one of three types of test.

The type of test designates the type of form used to enter the data.

Therefore when my drop down select type 2 from its field or if the user uses the navigation to move through the records, the system must look at the drop down and see if it = the number 2 if it does then close the "Main" form and open the "Superform" (The name is abitrary).

Likewise if the user moves off the "Superform" using the nave buttons or changing the state of the drop down the reverse is applied, the Superform closes and Main opens.
-----

Sounds simple right?

So we need to look at the Main form after its been selected using the nav buttons examine the drop down see if its a "2" and close main, open Superform.
OR
If the state of the dropdown changes to a "2" then close main, open superform
Else
do nothing

This is the code I'm using on the superform when the drop down state is changed which works fine.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CBO_MachineSpecification_AfterUpdate()
  2.  
  3. If ([MachineSpecification] = 2) Then
  4.  DoCmd.OpenForm "Superform", acNormal, , , acFormEdit, acWindowNormal
  5.   DoCmd.Close acForm, "Main", acSaveYes
  6. Else
  7.   DoCmd.OpenForm "Main", acNormal, , , acFormEdit, acWindowNormal
  8.     DoCmd.Close acForm, "Superform", acSaveYes
  9. End If
  10. End Sub
  11.  
If I reuse this code and add this to "Main"'s 'Current' event, so that it checks everytime the record is changed I get the following error

Run-Time error '2585' (This action cannot be carried out while processing a form or report event.) as the Main form attempts to be closed.

Huh? So Close takes no account of the state of the item it's closing? It doesn't wait until the form has completed its housekeeping before trying to shut it down?

Where am I going wrong?
Jul 26 '10 #1

✓ answered by NeoPa

Hiding and showing forms is not only more reliable, it also performs more smoothly. There is a certain overhead opening forms (though not always noticeable to the naked eye).

I suspect your current problem is related to trying this from within the Load event procedure. Try Open instead.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open()
  2.     Form_Superform.Visible = ([MachineSpecification] = 2)
  3.     Form_Main.Visible = (Not Form_Superform.Visible)
  4. End Sub

9 14180
patjones
931 Expert 512MB
Just wondering if there is any particular reason that you want to close out the main form when the secondary form opens up. I've got a similar situation in a couple of my databases at work and usually allow the main form to just stay open in the background.

That being said, I can say that in the past I have had problems trying to close a form from within it's own module.

If you attach this code to an event other than On Current, does the same problem result?

Pat
Jul 26 '10 #2
@zepphead80
There are two forms of test both require basically the same data however they are not entirely identical, and they are entered in different order.

The idea is to use a field in the database to change the format of the form (hide one open another).

The only event that seems to trigger when the inbuilt navigation is performed (in 2007) is on current.
Jul 26 '10 #3
Even more wierdness.

I had a brainwave perhaps I had answered my own question and instead of closing the form, just use its visible switch to hide it.

So I wrote this to deal with the first case senario (Record 1 who's MachineSpecification is = 2)

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Form_Main.Visible = True
  3. Form_Superform.Visible = False
  4. If ([MachineSpecification] = 2) Then
  5. Form_Main.Visible = False
  6. Form_Superform.Visible = True
  7. End If
  8. End Sub
  9.  
It unhides the "Superform" but doesnt hide the "Main" form. There is no logical reason why it shouldn't!


I'm beginning to think I should go back to assembly language, at least that IS logical!
Jul 26 '10 #4
NeoPa
32,556 Expert Mod 16PB
Hiding and showing forms is not only more reliable, it also performs more smoothly. There is a certain overhead opening forms (though not always noticeable to the naked eye).

I suspect your current problem is related to trying this from within the Load event procedure. Try Open instead.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open()
  2.     Form_Superform.Visible = ([MachineSpecification] = 2)
  3.     Form_Main.Visible = (Not Form_Superform.Visible)
  4. End Sub
Jul 26 '10 #5
@NeoPa
That doesnt do anything different to my original code even when added to the open form event.

PS that is some seriously wierd logic youre using there. Talk about code readability...not (Pun intended!)

Double click click Main to start the DBMS
Both the Main and the Superform display as open.
I ran the code in debug and as soon as it hits the endsub at the end of your code it reopens the Main form.

Could this have somthing to do with the focus?
Jul 26 '10 #6
NeoPa
32,556 Expert Mod 16PB
If that doesn't help I'm going to need to know how you structure your forms.

Are the two forms both opened by a third, or is one opened by the other?
Jul 26 '10 #7
@NeoPa
Each form opens the other, so the psuedo is;

Load form
Check form type required from machineSpecification field.

If machineSpecification = 2 load(unHide) form2 Unload(Hide)form1 else load form1 unload(Hide) form2

Of course form2 has the reverse.

I managed to get this going last night using your hide code but now it has created another problem.

When I swap forms it looses the previous forms position becomming one record out of sync.

I'm going to need to figure out how to detect which Nav button is press (forward/backward) and transfer the current record + 1 (or minus 1)to the next form.

Ive considered dumping the inbuilt Nav buttons but is need the general search it provides.

(This is access 2007 by the way)
This thread is getting off topic so Ill open another one for this query. Thanks for the help.
Jul 27 '10 #8
NeoPa
32,556 Expert Mod 16PB
No worries.

Feel free to post a link in here to the new thread and I'll go there as a priority.
Jul 27 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kimelia | last post by:
I want to create something like wizard, which will ask one question and proceed to the next question. So, it's like showing Form A (Question 1), then when the user press "Next" button, I want to...
2
by: Paul | last post by:
Hi this is related to a previous post, hopefully just a bit clearer description o the problem. I have a parent form that opens a new form (child form) while still leaving the parent form open....
5
by: prob | last post by:
I would like to acheive the following VB6 code in VB.NET Code in the Form1 - Button1_Click Dim obj1 as new form2 obj1.Prop = myprop obj1.show unload me
4
by: DraguVaso | last post by:
Hi, I want a Login-form that, once the username and password are validated, opens a new Form (of the 'main'-application), and closes itself. I tryed it like this: putting the following code in...
3
by: Karan | last post by:
I am calling finalize when form2 loads and deactivates form1 which closes form1. However, same thing is not happening in form2 because finalize is already called. Does anybody has solution to it....
4
by: ghadley_00 | last post by:
Hi, Can anyone recommend a piece of VBA code I could attach to a button in a MS Form form that will close the current form and bring the switchboard to foreground. I'm trying to integrate a form...
2
by: MLH | last post by:
Suppose that code running on FormB is moving the focus around on FormA to various textbox controls on FormA - Which form is the current form during this process? Is it FormA, which has the...
4
by: Jacko7289 | last post by:
Hi, I've got a form (SearchForm1) which searches the student number using the surname, given name, date of birth. Once the student number is shown i want the user to click a button which will close...
3
by: godhulirbalaka | last post by:
Dear Sir/Madam, I am new vb 6.0 user. I am developing Shop Management Program. I have a main form with buttons and menus. when i click any button then respective form is open. I want to set a...
6
by: dotnetnovice | last post by:
Hi everybody. I need some help in c# forms problem which is i have three forms naming main form,form1 & form2. main form is the mdiparent of both form1 and form2. i also have a toolstrip in my...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.