Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In VB6 when a program runs, as soon as you reference a form via it's form name VB6 creates an instance of that form. That instance is also added to the Forms collection.</p> <p>You can discover which forms have been instantiated using this code or a suitable variation thereof:</p> <pre><code>Dim frmCurrent As Form Dim output As String For Each frmCurrent In Forms output = output &amp; frmCurrent.Name &amp; vbCrLf Next MsgBox output </code></pre> <p>So when you call Form2.abc() you are calling the abc procedure on the newly created instance of the Form2 form (you can substitute the word class for form if it helps your understanding). </p> <p>When it comes time to exit the program you can get a type of error where the program is hanging around in memory but is not visible on the screen because you have closed all the visible forms but not the ones instantiated via calls like <code>Form2.abc</code>. This lead to the popular 'close all forms' code being added to the exit procedure of many VB6 programs:</p> <pre><code>Private Sub cmdExit_Click() Dim current As Form Dim output As String For Each current In Forms Unload current Next End Sub </code></pre> <p>When you dimension a variable and assign a new instance of Form2 to it you are creating a new form with scope according to the variable. The instance is <strong>not</strong> added to the Forms collection:</p> <pre><code>Dim frmNew As New frmTest frmNew.abc Dim frmCurrent As Form Dim output As String For Each frmCurrent In Forms output = output &amp; frmCurrent.Name &amp; vbCrLf Next MsgBox output 'Does not include frmNew aka frmTest </code></pre> <p>Thus your second method is generally the better one as it doesn't create an instance of Form2 in the Forms collection or reuse an exisiting instance that may give you an unexpected result.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload