How to prevent subform closure on main form window resize

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    How to prevent subform closure on main form window resize

    I have noticed that when I resize the window of my main form, that any subforms that "fall" out of the visible area get closed.

    Now Access automatically reopens them if I expand the window again, but I would still prefer to keep them "active" and in memory.

    Do anyone know a way in which to prevent access from closing them?

    And on the same note, if there is a way to detect this kind of closure, compared to closure that happens when the main form closes.
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    Smiley,

    I have never seen this behavior myself, but I have run into tons of problems with user resizing the screen. Here are a few of the solutions I have come up with.

    1). I wish Access would allow you to set the min/max height and width of a Form, much like C# does, you can mimic this though by hooking into the Forms Resize event and preventing them from reducing the size beyond a certain point. This has a side effect that the screen flashes and doesn't look all that great, but is doable.

    2). My prefered method is to dynamically resize the subforms/list boxes at run time to fit the size of the form. This works well if your subforms are in a good location. Again you hook into the Forms Resize event. I have even taken it so far as if they resize to a point that the subform won't appear I set the subform to be invisible. I believe this could work in your situation, set the subform to invisible and move it to the top left.

    Comment

    • TheSmileyCoder
      Recognized Expert Moderator Top Contributor
      • Dec 2009
      • 2322

      #3
      Hi Hype, and thank you for your reply.

      Yes, I do agree with 1. However, Im curious as to how you prevent the users from decreasing beyond a certain point, could you elaborate on your method?

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        All it takes is 2 lines of code. I am at home right now so I don't have any of my Access code so it might have misspelling, but it should give you an idea of what to do.

        Code:
        const minHeight as Integer = 10000 'Or whatever your min height is for your form
        
        if(me.windowHeight < minHeight) Then
        Docmd.MoveSize ,,,minHeight
        End if
        Me.WindowHeight is a read only property so you cannot change modify that directly which is why you have to use the MoveSize command. You are going to want to place this in your Form ReSize Event.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Shouldn't the close or unload event of the form trigger even if it's being used in a subform?

          Comment

          • TheSmileyCoder
            Recognized Expert Moderator Top Contributor
            • Dec 2009
            • 2322

            #6
            Ah yes, using the Docmd.MoveSize. I had tried using the WindowHeight, but as you say, I found it to be read only.

            @ Rabbit:
            The close event of the subform does trigger, however it my problem comes from not wanting the subform to close. There are times where I would like to reference the subform, even if it is not part of the visible area (And thus access has closed it).

            Comment

            • TheSmileyCoder
              Recognized Expert Moderator Top Contributor
              • Dec 2009
              • 2322

              #7
              Well I tried your suggestion, using the DoCmd.MoveSize, and while it prevents the user from KEEPING the form too small, it still allows him momentarily to resize the form to just the blue windows bar, which causes the subform to close. DAM!

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                If it closes, then the unload event should trigger first correct? You cancel the unload event and that will prevent the form from closing.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  Of course, you would then need a way close that subform. Perhaps a close button on the main form paired with a global variable will do the trick.

                  Comment

                  • TheSmileyCoder
                    Recognized Expert Moderator Top Contributor
                    • Dec 2009
                    • 2322

                    #10
                    You cannot cancel the unloading of a subform. While the event does fire, setting the Cancel=true seems to have no effect at all.

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #11
                      What about a DoCmd.Maximize that reruns if click restore?

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        Also, a form can be opened as a dialog, this prevents the user from resizing the form.

                        Comment

                        • TheSmileyCoder
                          Recognized Expert Moderator Top Contributor
                          • Dec 2009
                          • 2322

                          #13
                          Hi rabbit and thank you for all your replies, and tips.

                          The whole problem started when I wanted to write some code to allow dynamic form resizing (resizing the window makes the form resize as well as the controls). So placing it in a dialog would defeat the purpose.

                          On even further examinitation, I've found that the subform closing happens when the detail area of the main form is no longer visible (I.e the height of the main forms window gets so small to only allow space for the header and footer.

                          I remember having had this issue before, in a search form, where I put all the search fields in the forms header, then search results would be displayed in the detail area. I added an button with a Docmd.MoveSize which basicly shrunk the form, to only show the header area, in which there was also buttons to navigate the search results, in the main form. One of the issues I had there was that the subform displaying the search results would also unload.

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            Well, the only thing I can think of now is to create a 1 pixel wide copy of the subform and put it in the top left corner of the header. I don't know the repercussions of that though.

                            Comment

                            • TheSmileyCoder
                              Recognized Expert Moderator Top Contributor
                              • Dec 2009
                              • 2322

                              #15
                              I could try playing with moving it to the header, however im not sure If I can catch the correct time to move it to the header.

                              One of the issues is that the form looses its "state" so to speak. Through other controls, I manipulate the Recordsource of the form, and when its closed (by the resize) and opened again (by the resize) it can no longer remember its recordsource. Alternativily, I will just have to try and deal with this problem instead.

                              Comment

                              Working...