Browse DevX
Sign up for e-mail newsletters from DevX

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



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

Set Forms to Nothing with Caution

It's a good idea to set your form variables to Nothing to recover all memory that was allocated for the form module. Executing Set Form1 = Nothing for a loaded form, however, will have no apparent effect, but will leave the form module in a confused state. You can demonstrate this by executing:

Set Form2 = Nothing
Msgbox Forms.Count & " loaded forms"
Unload Form2
Unload Form2

The second line of this code seems to do nothing, but the second use of Form2's Show method will in fact load and display a second instance of Form2. The forms collection will know about both instances, but only one of them will be unloaded with the Unload statement.

To avoid these problems, always be sure a form is unloaded before setting it to Nothing. While you cannot execute Set Me = Nothing, you can achieve the same effect in a form's Unload event:

Form_Unload (Cancel As Integer)
        Dim Form As Form
        For Each Form In Forms
                If Form Is Me Then
                        Set Form = Nothing
                        Exit For
                End If
        Next Form
End Sub

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