How To Close Forms Properly In Visual Basic 6

Yes, it’s 2018, and I still consult in Visual BASIC 6. Microsoft’s perennial product for programmers is alive and kicking, although mostly for software maintenance, not new builds (I feel my age when I realize I’ve worked with it now for 23 years, since 1996…) And while it has some advantages over the current crop of languages (speed and ease of delivery, for instance, unlike .net) it does have one very big issue – documentation for it is shrinking every year.

I’ve done my best by scanning eBay for any old books, but sometimes you just have to roll up the sleeves and test things yourself.

For instance, this month the question arose: Just how do you kill a form?

Normally, it’s not an issue – once you have your project set up, forms appear magically, and then they disappear the same way.

It gets trickier when you have more than one, or use SET to create a form object.

In any case, after I scoured my books and the Internet, I decided to do a simple app, using Form1 through Form3 (with Form1 the main one), tried different ways to create them, and then displayed the results when closing them. The results were surprising:

  • You don’t need an “Unload Form” unless you do something with the form. VB6 forms have two parts – the data and the form itself. Until you change the data or do something with the form, the data needs no cleanup, so form closings aren’t a problem. Do something as simple as Form2.Caption=”X” though, and VB generates that form, which then needs to be gotten rid of. And while it will try to remove it at the end, it’s best to use an Unload.
  • For safety, add a “Set Form=Nothing”. If you’ve created a form and set it, unload should work – until it doesn’t. I found that if you used a local variable to set up a form, there was no link for unload to use to get rid of it, so you had a problem. Of course that was a bit contrived – who sets up a form without keeping the form variable? But still, for added safety, setting the form to Nothing will force cleanup.

How to combine the two? The easiest way is to use a function like this:

Private Sub CloseAllBut(ByVal formNameLeftOpen As String)
  Dim frm As Form
  For Each frm In Forms
    If frm.Name <> formNameLeftOpen Then
      Unload frm
      Set frm = Nothing
    End If
  Next
End Sub

Then use this call in your main form’s Unload event:

CloseAllBut(Me.Name)

The function will close everything but the current form, regardless of whether the form’s variable is still around, and no matter how the form was created.

Of course, this function will allow the program to close, but doesn’t explain why it doesn’t close on its own. For that, you can use another function, which lists the name in a ListBox1 on your form, to help troubleshoot:

Private Sub ListOpenForms()
    Dim frm As Form
    List1.Clear
    For Each frm In Forms
      List1.AddItem (frm.Name)
    Next
End Sub

Even in 2018, VB6 holds some mysteries. But if you use Unload and Set form variables to Nothing, then at least this won’t be a mystery any longer…

Comments are closed.