vbavb6

VB6 referencing the class name of a form instead of an object variable?


I'm maintaining a VB6 application, and I want to understand what this means:

' somewhere there's a form defined frmMyForm
Sub Main()
  Load frmMyForm
End Sub

' elsewhere
frmMyForm.Show

The thing I don't understand is there isn't a frmMyForm object Dim'd anywhere. It's just referencing the name of the user form. I know you can also Set foo = New frmMyForm and foo.Show and it'll work too - I've tried this in an immediate window.

What's going on here? Are the names of forms just predeclared global variables or is there something I'm missing?

I understand it's similar to VBA but not identical. I looked at the Load statement and I'm not sure if it's even "required".


Solution

  • The VB6 forms are automatically created as global. This global instance is accessible by the form's name, like frmMyForm in your example.

    Load frmMyForm: This loads the form into memory(execute the frmMyForm_Load sub) but does not display it. It's useful if you need to initialize something in the form before showing it.

    When you use Set foo = New frmMyForm, you're creating a new instance of the form, which is different from the global instance. This is useful if you need multiple instances of the same form, but you must use Unload Me inside the form or when outside the form use:

    Unload foo
    Set foo = Nothing