Solved

Use Multiple Forms

Posted on 2016-08-23
4
69 Views
Last Modified: 2016-08-24
Why first form visible after last command in :

Public frm() as New Form1
Sub Main
  Redim frm(0)

 frm(0). Show

 Redim frm(1)
 frm(1).Show
....
 Unload frm(1)
set frm(1)=Nothing

,,,
unload frm(0)
set frm(0)=Nothing

Debug.Print Forms.Count resut 1 , I beleave but must be zero, no ?
0
Comment
Question by:mancio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 48

Expert Comment

by:Martin Liss
ID: 41767058
Try it like this.
'Public frm As New Form1 'Note that this is commented out
Sub Main()

Dim f As Form
 ShowForm

 ShowForm

For Each f In Forms
    If f.Tag = "1" Then
        Unload f
        Set f = Nothing
        Exit For
    End If
Next
For Each f In Forms
    If f.Tag = "2" Then
        Unload f
        Set f = Nothing
        Exit For
    End If
Next


Debug.Print Forms.Count ' resut 1 , I beleave but must be zero, no ?
End Sub
Sub ShowForm()
Dim frm As New Form1
frm.Show
frm.Tag = Forms.Count
End Sub

Open in new window

0
 
LVL 48

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 41767214
I just realized what the problem is with your code. You were Redim-ing without using the Preserve keyword. That caused the form array to be rebuilt rather than just added to. This works. See line 7.

Public frm() As New Form1
Sub Main()
  ReDim frm(0)

 frm(0).Show

 ReDim Preserve frm(1)
 frm(1).Show

 Unload frm(1)
Set frm(1) = Nothing

Unload frm(0)
Set frm(0) = Nothing

Debug.Print Forms.Count ' resut 1 , I beleave but must be zero, no ?
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:mancio
ID: 41767422
Thanks Martin Liss,
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 41767674
Please don't forget to choose one of my solutions as the answer.

In any case, you're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 3 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question