none
How do I close a form RRS feed

  • Question

  • I have a form, which needs to be initialized when I change worksheet. Is their a code which finishes the job: UserForm_Terminate?


    :) Søren

    Wednesday, January 7, 2015 10:33 AM

Answers

  • Hi Søren

    I would suggest you go in these two ways:

    1. In the form's Activate event handler, initialize the controls and data on the form. 

    2. Initialize the form in the form's Initialize event handler. Every time you don't need this form, set it to Nothing, and create a new instance of this form when you need it. 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 2, 2015 1:54 PM
    Moderator

All replies

  • Hello Soren,

    Do you develop an add-in or VBA macro?

    Could you please be more specific?

    Wednesday, January 7, 2015 10:37 AM
  • VBA macro.

    I would like to simulate the action, when You hit the cross in the upper right corner, in a macro…

    e.x. FrmLog.close (which do not exists)

    :) Søren


    :) Søren

    Wednesday, January 7, 2015 11:38 AM
  • If u've 2 forms,

    If u've created any button controls in form1 double click on that button a method will be generated under that method write the following code..

    Ex..Form1.cs

        {

           this.hide() (or) form1.close();

           Form2 f2=new Form2();

           f2.show();

          }

    Form2.cs

    {

    //Some code over here

    }

             


    Thanks & Regards RAJENDRAN M

    Wednesday, January 7, 2015 12:04 PM
  • Unload userform is the command to close the userform or me.unload userform.hide will hide the form but not close it, if you need it again and don't what to repopulate it.

    See How to Temporarily Hide a UserForm for more information.

    Wednesday, January 7, 2015 12:12 PM
  • Thank You for answering…

    The unload userform closes the form, but You can't reopen it with userform.show.

    What I need is a reset the userform: When I hit the (closing) cross and run the macro userform.show the form initializes again and display the required content (depending on which sheet it opens from). The userform.hide does not initialize the form…

    Any suggestions?

    :))


    :) Søren

    Wednesday, January 7, 2015 1:28 PM
  • Hi Søren

    I would suggest you go in these two ways:

    1. In the form's Activate event handler, initialize the controls and data on the form. 

    2. Initialize the form in the form's Initialize event handler. Every time you don't need this form, set it to Nothing, and create a new instance of this form when you need it. 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 2, 2015 1:54 PM
    Moderator
  • Thank You for Your suggestions.

    I'm not sure how to follow Your advise, please help me with the commands:

    Private Sub UserForm_Initialize()

    FrmLog.?

    End Sub 


    :) Søren

    Wednesday, February 11, 2015 10:53 AM
  • Typically the purpose of a Form is to display information to the user and accept inputs. When done the User will close the form with little x or click a Close button on the form which calls

    Unload Me

    If you want to close the form, perhaps after some sort of timeout, there are different ways depending whether you have shown the Form as Model or Modeless.

    If you showed it as Model your code loaded the form is suspended

    UserForm1.Show ' code stops here until the form is closed

    The only way to close the form is via code in the form, typically called by the user.

    Code that's called by buttons in the form works normally, and at any part of that code can branch off and do

    Unload Me ' code returns to the line that loaded and showed the form

    However things are different with modeless forms

    UserForm1.Show vbModeless ' code continues until it terminates

    In another routine (not in the form) you can close the form like this

    Sub test()
        UserForm1.Show vbModeless
    
    End Sub
    
    Sub CloseForm()
    Dim uf As UserForm
        For i = UserForms.Count - 1 To 0 Step -1
            If UserForms(i).Name = "UserForm1" Then
                Unload UserForms(i)
            End If
        Next
    End Sub

    Note the index of the first userform is zero.

    Another and perhaps better way is to set a reference to the form when loading it, and use the same reference to unload it. I hesitate to give an example because it's important to ensure all object references are cleaned up in all scenarios.

    Also look at the form's QueryClose event

    Wednesday, February 11, 2015 3:33 PM
    Moderator
  • Thank You Peter – I will try it…

    J


    :) Søren

    Friday, February 13, 2015 10:37 AM