?
Solved

Unload Forms in Module

Posted on 2003-03-10
10
Medium Priority
?
300 Views
Last Modified: 2013-12-25
I have several MDIChild Forms.
And I would like to unload them through a Function in the Module.
This is how it looks:

Public Function CloseForm(frmName As String)
Dim tempName(4) As String
Dim x

tempName(0) = "form1"
tempName(1) = "form2"
tempName(2) = "form3"
tempName(3) = "form4"

For Each x In tempName
    If x <> frmName Then
        Unload (x)
    End If
Next
End Function

There will be a form name passed into the function. I wouldn't unload the form.
Only the other forms in the array.
However, I have an error: Run-Time Error '424'.
Anyone can help me up?
Thank you! :)
0
Comment
Question by:bobubi
[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
10 Comments
 

Expert Comment

by:bhnv9
ID: 8103142
To unload all mdi child forms:

Dim frm As Form
For Each frm In Forms
    If frm.Caption <> "main form caption" Then
         Unload frm
     End If
Next
0
 

Expert Comment

by:bharat_mane
ID: 8103286
Hi bobubi ,
   I tried the same but here the scope concept is coming....
See I think u might be declearing and loading the forms in the some form's code isn't it? And u are trying to access the form variable in module... But it wont work because the form which you are creating is the local to that form where u are creating and declearing.
        But If you are declearing the form's variable name in the module itself then u can instantiate the form in the module itself as Load and visible as well as you can unload it in module itself where you are declearing it. In this case the form variable is local to module. But now you wont be able to use the form other then this module as this is local to this module.
       But Still you can achive this with declearing the form's variable name as global so you can load it from anywhere and unload it from anywhere like  other forms.
       It is similer to the other forms.....
e.g Suppose u have added one form through Project-Add Form menu. This form you can Unload in module because this is global to application.
      Similarly  if you want to create the forms programatically and want to use throught the projects, in module too you have to decleare them as global in any module
e.g.   Global frm1 As Form1
       'It means u are declearing frm1 as type form1
       'Then whereever you want create it u can
       Set frm1 = new Form1
       Load frm1
       frm1.Visible = true
       'In the same way you can unload it anywhere in the projet even in module by simply
       Unload frm1

Hope this will help you.
Let me know in any other issues

Regards
Bharat

 
 
 
0
 
LVL 7

Expert Comment

by:Smallint
ID: 8103327
One possible solution for your problem:

Public Function CloseForm(frmName As String)
Dim tempName(4) As String
Dim x
Dim n As Integer
Dim Found As Boolean

tempName(0) = "form1"
tempName(1) = "form2"
tempName(2) = "form3"
tempName(3) = "form4"

For Each x In tempName
   If x <> frmName Then
       
       Found = False
       For n = 0 To Forms.Count - 1
          If Forms(n).Name = x Then
            Found = True
            Exit For
          End If
       Next
       If Found Then
            Unload Forms(n)
       End If
   End If
Next
End Function
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:bobubi
ID: 8103443
I have tried all of your solutions.
Although it worked. But it didn't really help on my problem. I put a breakpoint while looping on the Form Names, however I couldn't see the 4 form names that I wanted to Unload. Could that be because I haven't loaded the forms yet? Because out of the 4 forms only 1 form was loaded.

Thank You :)
0
 

Author Comment

by:bobubi
ID: 8103461
I have tried all of your solutions.
Although it worked. But it didn't really help on my problem. I put a breakpoint while looping on the Form Names, however I couldn't see the 4 form names that I wanted to Unload. Could that be because I haven't loaded the forms yet? Because out of the 4 forms only 1 form was loaded.

Thank You :)
0
 
LVL 7

Expert Comment

by:Smallint
ID: 8103482
In forms collection there are all forms loaded. If forms you want to unload there aren't, you haven't loaded the forms yet.
0
 
LVL 7

Expert Comment

by:Smallint
ID: 8103488
In forms collection there are all forms loaded. If forms you want to unload there aren't, you haven't loaded the forms yet.
0
 

Author Comment

by:bobubi
ID: 8103528
I think I got what you mean SmallInt.
My intention was to unload the forms when they are all loaded up next time. But initially, only 1 form will load when the form1 button is clicked. The function in the module will then initiate, hence unloading the other forms. Do you understand what I say?

It's actually closing the other forms while maintaining the active form. While form2 button is clicked. Form 2 will appear and the previous form will unload itself.
0
 
LVL 7

Accepted Solution

by:
Smallint earned 150 total points
ID: 8103723
I think I understand you ;) You want to start unloading only when all forms are loaded? That's ok?

Then function should be:

Public Function CloseForm(frmName As String)
Dim tempName(4) As String
Dim x
Dim n As Integer
Dim Found As Boolean
Dim FormsFound As integer


tempName(0) = "form1"
tempName(1) = "form2"
tempName(2) = "form3"
tempName(3) = "form4"

FormsFound=0

For Each x In tempName
      For n = 0 To Forms.Count - 1
         If Forms(n).Name = x Then
           FormsFound = FormsFound + 1
           Exit For
         End If
      Next
next

if FormsFound = Ubound(tempName)+1 then

For Each x In tempName
  If x <> frmName Then
       
      Found = False
      For n = 0 To Forms.Count - 1
         If Forms(n).Name = x Then
           Found = True
           Exit For
         End If
      Next
      If Found Then
           Unload Forms(n)
      End If
  End If
Next

end if
End Function
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9053216
This question has been classified as abandoned.  I will make a recommendation to the moderators on its resolution in a week or two.  I would appreciate any comments by the experts that would help me in making a recommendation.

It is assumed that any participant not responding to this request is no longer interested in its final deposition.

If the asker does not know how to close the question, the options are here:
http://www.experts-exchange.com/help/closing.jsp

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 Month14 days, 1 hour left to enroll

800 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