Search code examples
excelvbauserform

Problem with showing a UserForm after termination


1st, what i want to do:

I have a main UserForm, on that form i have a button to show secondary UserForm. When i click on that button, i want main form to be hidden. When i'm done with with work on the secondary form, i want to close it and show the main form again.

2nd, what i have so far (relevant code):

Main form code:

Private Sub createFastButton_Click()
    Me.Hide
    formSec.Show
End Sub

Secondary form code:

Private Sub cancelButton_Click()
    Me.Hide
    formMain.Show
End Sub
Private Sub UserForm_Terminate()
    formMain.Show
End Sub

3rd, the problem:

If i use ControlButtons to navigate between forms, all works as intended. But if i use the "X" close window button on the secondary form and try to open it again from main form, it loses all functionality (main form works fine). It just shows the secondary form as i see it, when i use "View Object" in VBA editor. None of the buttons work and none of the intended boxes and labels are filled. Even the "X" button doesn't work. For me it seems obvious, that the problem is with unloading of secondary form. I tried to replace Me.Hide in secondary form with Unload Me and exactly same thing happens, as if i press the "X" button. So i need to do something with Sub UserForm_Terminate(), i tried to add Me.Hide there and as expected, it did nothing.

I there a solution to my problem?

Thx in advance.


Solution

  • main routine, calling Main Form:

    With New formMain
        .Show
    End With
    

    main form, calling secondary form

    Private Sub createFastButton_Click()
        Me.Hide
        With New formSec
            .Show
        End With
        Me.Show
    End Sub
    

    secondary form

    Private Sub cancelButton_Click()
        Me.Hide
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
       If CloseMode = VbQueryClose.vbFormControlMenu Then Cancel = True
    End Sub
    

    no need for UserForm_Terminate() in secondary form, unless for unsaid needs