Consulting

Results 1 to 16 of 16

Thread: Form Close help

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    Form Close help

    I have 2 forms. How can I have it for if someone clicks the upper red X on the right of one of the forms it will show the other form.

    Example

    Closed Userform1 Then Show UserForm2

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]Private Sub UserForm_Terminate()
    AppActivate UserForm1.Caption
    End Sub[/VBA]

  3. #3
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    That doesnt' seem to work. Are you sure it's .caption?

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Well, be sure that you put it in the right Terminate event for the other userform.

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    I would have thought that the OP is requesting when you close the first form by using the red X, the second form then shows.

    How does .caption function?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then

    UserForm2.Show
    End If
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    AppActivate will activate the window with that caption. The advantage over the Show method is that it does not reset the window position.

    Of course you should probably check to see if the window is even open or code for an error. While no error would occur with the Show method, one want to only use it if the userform is open already as well.

    One issue for me seems to be that both methods do not put the focus on the 0 index control, even when .Setfocus is used.

    The QueryClose event is better than the Terminate event for the OP's needs.

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Thanks Kenneth
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Kenneth

    Why use AppActivate?

    What if the form isn't open or loaded?

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Norie
    Kenneth

    Why use AppActivate?
    That seems sufficient enough a question to me, no need to qualify further. Totally unnecessary, totally redundant.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    xld

    That's a bit cryptic.

    Is that post for me, the OP or Kenneth?

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yeah, after I posted it I figured it was. It was meant to affirm your comment, but also to suggest that asking why was sufficient, no need to justify that question. In other words, IMO AppActivate was totally the wrong suggestion.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If a solution works, I am not sure how that is bad. In post 7, I explained that something like the code below would be needed for a better routine. I guess it is up to the user to decide if they want to force a reposition using show just to re-activate the userform. For my tests, I assumed that ShowModal was False and moved focus to a cell before the close.

    Even so, the focus to a control after the close to activate or show seems to be more of a concern to me.

    If ShowModal=True for both userforms, then closing the 2nd and showing the 1st would be a non-issue.

    Private Declare Function FindWindow Lib "user32" _
      Alias "FindWindowA" (ByVal lpClassName As String, _
      ByVal lpWindowName As String) As Long
      
    Private Sub CommandButton1_Click()
      Unload Me
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
      If CloseMode = 0 Then
        If FindWindow("ThunderDFrame", UserForm1.Caption) Then AppActivate UserForm1.Caption
      End If
    End Sub

  14. #14
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Kenneth

    Why bring Windows API or AppActivate in to this?

    Surely what's being dealt with is Excel VBA userforms.

  15. #15
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I guess post #7 was too complicated. After reading it again, I see that my grammar was not the best. I try to correct grammar and spelling mistakes when I find them and a forum has not locked the post by then.

    So, to explain my reasoning a bit more as requested: I often consider other issues that someone else like the Original Poster might encounter. I guess they can always post back when they encounter that issue but I might be absent by then. Some people like this approach and some don't. In other words, I do my best to help and it is certainly not perfect. To recap post #7 and why I used FindWindow. I wanted to give the user an option to show or NOT show userform1. FindWindow is the first method that came to mind. After thinking on it a bit more, the userform's Visible property is the better method for that part.

    Again, see post #7 for why I used AppActivate rather than Show.

    All methods have advantages and disadvantages over other methods. You guys seem to like the Show method better than AppActivate. I leave it to each user to decide what they like best.

    I hope that you found my reasoning helpful.
    Last edited by Kenneth Hobs; 08-16-2008 at 08:23 PM.

  16. #16
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    This thread is heading in a non positive direction, therefore I'm closing it. Should the OP want an opportunity to post a responce to the advice given in the earlier posts, then please PM me.

    With a passage of time, clearer minds will prevail.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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