Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

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! :)
0
bobubi
Asked:
bobubi
1 Solution
 
bhnv9Commented:
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
 
bharat_maneCommented:
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
 
SmallintCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
bobubiAuthor Commented:
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
 
bobubiAuthor Commented:
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
 
SmallintCommented:
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
 
SmallintCommented:
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
 
bobubiAuthor Commented:
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
 
SmallintCommented:
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
 
GPrentice00Commented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now