469,300 Members | 2,255 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

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 13309
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,173 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,173 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,173 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

Post your reply

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

Similar topics

3 posts views Thread by Karan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.