Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 7, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Close All Forms Before the End of a Program

It is well known that VB3 is not always as conscientious as it should be about unloading all the forms in an application whenthe application terminates. Because unloading all the forms for an application manually can be tricky, I have developed a small routine that can be called as a prelude to the End statement:

Sub Main ()
' Blah, blah, blah...our code here....

End Sub

Sub CloseForms()
        Dim iFormCount As Integer
        Dim i As Integer
        'Nothing's gonna stop us now....
        On Error Resume Next

        'Store the number of forms NOW 
        'because the count will change as 
        'we close them
        iFormCount = Forms.Count - 1

        'Step downward through the Forms 
        'Collection to ensure we get 
        'ALL of them...
        For i = iFormCount To 0 Step -1
                Unload Forms(i)
                Set Forms(i) = Nothing 
End Sub

The key here is to get the count of the number of open forms and then loop from that number down to make absolutely sure that all forms in the application are unloaded.

Joe Ackerman
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date