Solved

Use Multiple Forms

Posted on 2016-08-23
4
57 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
  • 3
4 Comments
 
LVL 46

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 46

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 46

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 59
Getting warning: You are about to delete 1 row(s) 9 48
How to make an ADE file by code? 11 83
Send outlook email from VBS Script 2 29
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

770 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