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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Microblitz
    New Member
    • Jul 2010
    • 40

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

    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.

    Code:
    Private Sub CBO_MachineSpecification_AfterUpdate()
    
    If ([MachineSpecification] = 2) Then
     DoCmd.OpenForm "Superform", acNormal, , , acFormEdit, acWindowNormal
      DoCmd.Close acForm, "Main", acSaveYes
    Else
      DoCmd.OpenForm "Main", acNormal, , , acFormEdit, acWindowNormal
        DoCmd.Close acForm, "Superform", acSaveYes
    End If
    End Sub
    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?
  • patjones
    Recognized Expert Contributor
    • Jun 2007
    • 931

    #2
    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

    Comment

    • Microblitz
      New Member
      • Jul 2010
      • 40

      #3
      Originally posted by zepphead80
      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
      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.

      Comment

      • Microblitz
        New Member
        • Jul 2010
        • 40

        #4
        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 MachineSpecific ation is = 2)

        Code:
        Private Sub Form_Load()
        Form_Main.Visible = True
        Form_Superform.Visible = False
        If ([MachineSpecification] = 2) Then
        Form_Main.Visible = False
        Form_Superform.Visible = True
        End If
        End Sub
        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!

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32661

          #5
          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.
          Code:
          Private Sub Form_Open()
              Form_Superform.Visible = ([MachineSpecification] = 2)
              Form_Main.Visible = (Not Form_Superform.Visible)
          End Sub

          Comment

          • Microblitz
            New Member
            • Jul 2010
            • 40

            #6
            Originally posted 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.
            Code:
            Private Sub Form_Open()
                Form_Superform.Visible = ([MachineSpecification] = 2)
                Form_Main.Visible = (Not Form_Superform.Visible)
            End Sub
            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...n ot (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?

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32661

              #7
              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?

              Comment

              • Microblitz
                New Member
                • Jul 2010
                • 40

                #8
                Originally posted by NeoPa
                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?
                Each form opens the other, so the psuedo is;

                Load form
                Check form type required from machineSpecific ation field.

                If machineSpecific ation = 2 load(unHide) form2 Unload(Hide)for m1 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.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32661

                  #9
                  No worries.

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

                  Comment

                  • Microblitz
                    New Member
                    • Jul 2010
                    • 40

                    #10

                    Comment

                    Working...