Consulting

Results 1 to 8 of 8

Thread: User Forms behaving strangely

  1. #1
    VBAX Regular
    Joined
    Feb 2005
    Posts
    82
    Location

    User Forms behaving strangely

    I have four forms, one is the Master form that has three buttons to call the others. When a form is called, the Master is hidden and the other is shown. When one of the others is closed, the Master is opened again. Here is where the problem comes up, if I call the same form a second time, the code seems to freeze on the last line of code. Here's the code:
    [vba]'Click button, form shows
    Private Sub btnForm1_Click()
    StartUp.Hide
    form1.Show 'If I call the form a 2nd time, the code freezes here and I have to break in to stop it.
    End Sub

    'When form is closed, Master form is shown.
    Private Sub UserForm_Terminate()
    Unload form1
    StartUp.Show
    End Sub
    [/vba]

    I've tried changing Unload to Hide and vice versa, and it doesn't make a difference. Any thoughts?

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why not unload the StartUp form rather than just hiding it?

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Unload is not performed until the end of a proceedure.
    If that terminate event is in form1, [VBA]StartUp.Show [/VBA]is performed before form1 is unloaded.

    You might try (in StartUp)
    Private Sub btnForm1_Click() 
        StartUp.Hide 
        form1.Show 
        form1.Unload
    End Sub
    (in form1)
    [VBA]Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = true
    Me.Hide
    StartUp.Show
    End Sub[/VBA]

  4. #4
    VBAX Regular
    Joined
    Feb 2005
    Posts
    82
    Location
    Quote Originally Posted by mikerickson
    You might try (in StartUp)
    Private Sub btnForm1_Click() 
        StartUp.Hide 
        form1.Show 
        form1.Unload
    End Sub
    (in form1)
    [vba]Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = true
    Me.Hide
    StartUp.Show
    End Sub[/vba]
    Still doesn't work. The code still gets hung up at form1.Show (but only the second time the form is called)

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    How are you dismissing form1?
    Also, are these UF's modal or modeless?
    When trying the code I posted, did you remove the form1_Terminate event from the first post?
    Is there a _Deactivate event for either form?

    (If you attach a workbook with example userforms that demonstrate your problem, please be aware that my Mac can't handle either ActiveX sheet controls or modeless userforms.)

  6. #6
    VBAX Regular
    Joined
    Jul 2008
    Location
    Cincinnati, OH
    Posts
    86
    Location
    It is better IMO to provide yourself with some explicit references and then take care of the cleanup yourself. It is the only way to know for sure that your form(s) is/are really gone.

    For example. Three forms. frmMain, Form1, and Form2. frmMain contains CommandButton1,2.

    Code in frmMain:
    Option Explicit
    
    Dim f1 As Form1, f2 As Form2
    
    Private Sub CommandButton1_Click()
        Set f1 = New Form1
        Set f1.MainForm = Me
        Me.Hide
        f1.Show
    End Sub
    
    Private Sub CommandButton2_Click()
        Set f2 = New Form2
        Set f2.MainForm = Me
        Me.Hide
        f2.Show
    End Sub
    Code in both Form1 and Form2:
    Option Explicit
    
    Private pMainForm As frmMain
    
    Friend Property Set MainForm(frm As frmMain)
        Set pMainForm = frm
    End Property
    
    Private Sub UserForm_Terminate()
        pMainForm.Show
        Set pMainForm = Nothing
    End Sub

  7. #7
    This seems to work for me. The following is i the main form and there are two buttons that call the two subforms

    [VBA]Private Sub CommandButton1_Click()
    Me.Hide
    SubForm1.Show
    Me.Show
    End Sub

    Private Sub CommandButton2_Click()
    Me.Hide
    SubForm2.Show
    Me.Show
    End Sub[/VBA]

    In the subforms, if you have a button that is used to exit them:

    [VBA]Private Sub CancelButton_Click()
    Unload Me
    End Sub[/VBA]

    Will also run if you just click the little x in the corner of the form. You don't need a terminate event in the subforms as the code in the main form should just continue running when the sub form is closed.

    Do let me know if I've missed something but this is the method I've been using when doing stuff likethis with userforms and so far have not had a problem with it

  8. #8
    VBAX Regular
    Joined
    Jul 2008
    Location
    Cincinnati, OH
    Posts
    86
    Location
    My method works rather the forms are modal or modeless...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •