(VB) Set frm = Nothing --> Why needed?

Posted on 2007-10-02
Last Modified: 2013-12-26
Dear Gurus
My VB6 app has about 50 forms.  An OCX supplier recenly sent me some sample code for his product, and each of the forms had
Set frm = Nothing (with 'frm' being the specific name of each form)
in the unload routine.

I was wondering why such code is useful?
(I understand from
that the code is designed to free resources - but doesn't VB automatically do that when forms are unloaded??)

THanks in advance
Question by:BrianBeck
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    if there is some variable of the name frm set to the form itself, you need that, unless the form will remain in memory, and the application will become invisible to the user as application, but still show up in the process list.

    Author Comment

    Hi angelIII
    THanks, and while I understand the second half of your answer, I havn't quite worked out what 'if there is some variable of the name frm set to the form itself,' means - can you please elaborate, usefully with a simple example.
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    in vb, you can declare a variable:
    dim v as <data type>
    public gV as <data type>

    you could do that for the form itself
    public frm as frm

    declaring a variable of the type of the form itself.
    now, even if you don't do that, vb6 does this for you in the background anyhow as soon as you access the form implicitely.

    is your form (frm) the project startup module?
    or do you start the form with the frm.Show

    -> the frm variable is implicitely created in the background by the vb compiler
    --> you need to clear it explicitely to be 100% sure.


    Author Comment

    Hi angelIII
    Thanks, and on start up, the main form activates, showing its menus etc.  Users can then click a menu item that calls up sub forms - and all these sub forms are called using the standard frm.Show method.

    Just to clarify:
    1. On application startup, is an instance of each form assigned to their respective 'implicitly declared variable'?
    2. Or, is the '.Show' method allocating memory for the form, and implicitly assigning it to that variable?

    It seems to me that the 'set frm = nothing' may not be needed.  I've not used used it for years, so am unclear as to whether I should start to use it.  You see, if I start to use this method, do I need to be extra careful in situations where a sub form calls another sub form?  Or, will VB6 happily deal with these mattes in its regular 'garbage collection'?
    LVL 142

    Accepted Solution

    to clarify: it's 2.
    vb6 does not have a garbage collector, every variable contents that is set to nothing is destroyed immediately

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now