Link to home
Start Free TrialLog in
Avatar of bobubi
bobubi

asked on

Unload Forms in Module

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! :)
Avatar of bhnv9
bhnv9

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
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

 
 
 
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
Avatar of bobubi

ASKER

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 :)
Avatar of bobubi

ASKER

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 :)
In forms collection there are all forms loaded. If forms you want to unload there aren't, you haven't loaded the forms yet.
In forms collection there are all forms loaded. If forms you want to unload there aren't, you haven't loaded the forms yet.
Avatar of bobubi

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Smallint
Smallint

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
https://www.experts-exchange.com/help/closing.jsp

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer