• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 148
  • Last Modified:

Removing a form from memory

I have a form in VB 6 which has several text boxes and combo
boxes on it.  When I exit this form I do a "unload me" and a "set frmapp = nothing".  The problem is that when I load the form again later in the program the data that was in it when I unloaded it is still there.  
The easy way to solve this problem is to write a procedure
that clears out all of the text boxes, but I thought unloading it and setting it to nothing would automatically do that.  
Any thoughts or suggestions would be great.
1 Solution
Where is the code where you are setting the frmApp = Nothing?  If it is inside of the frmApp, it may be possible that the form can't entirely be unloaded, as you're still executing code within that form.  Is this form modal?  If so, then you can hide it, which will then return control to the place in code where you first showed it modally, and on the next line you can unload it, then set it to nothing.

If it's not shown modally, you might try moving the set frmApp = nothing in the Unload event of the form frmApp.
'put your set form = nothing before the unload
'and If all else fails

'if you have only one form this will work fine in a click event
  Dim myCtrl as control
  For Each myCtrl In Me.Controls
      If TypeOf myCtrl Is TextBox Then
            myCtrl.Text = ""
      End If
    unload me

>the data that was in it when I unloaded it is still there
Not possible. You never Unloaded that form.

Setting form to nothing will clear all controls and form variables.
Unloading form will clear all controls, but form variables will still be in memory:

' Form1 code
Option Explicit

Private Sub Form_Load()
    Dim x As New Form2
    Load x ' this will load form2
    ' set form2 variable
    x.myVar = "www"
    ' set form2 text1.text
    x.Text1.Text = "wwwwwwwwww"
    Unload x
    Debug.Print "after unload: "
    Debug.Print x.myVar ' unload will not myVar from memory
    Debug.Print x.Text1.Text ' unload will clear textbox
    Set x = Nothing ' this will clear both form2 variables and controls
End Sub

' Form2 code, add textbox Text1
Option Explicit
Public myVar As String

Private Sub Form_Load()
End Sub

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now