[RESOLVED] How do you Declare Public Form and keep Data
I have a form that contains a Grid which I reference from a seperate procedure and populate with numbers. The first time I show that form I see all my numbers. If I close the form and then show it a second time the numbers are gone- I must populate it again. I can't figure out how to declare the Form Public so I don't lose the numbers data. Seems pretty trivial but I can't figure it out, what am I missing? Any guidance is greatly appreciated.
Re: How do you Declare Public Form and keep Data
I assume when you mean "close" the form you are unloading it. Of course, if you reload the form it starts from scratch. Don't unload the form just hide it temporarily. This will maintain the data. First use:
form1.hide
and later
form1.show
The data on the form will be maintained. At the end of the applicaition use:
unload form1 :bigyello:
Re: How do you Declare Public Form and keep Data
Thanks, that works fine if I put up a button to hide form. I have tried to make this work so the user can click form_unload (x in control box) and it will hide form instead of unloading it - 1st code below for Pool1Form, and then unload Pool1Form when user exits MainForm - 2nd code below. My program will not terminate successfully this way - it works and I can see Pool1Form close in immediates window but the program does not terminate. Any ideas?
'Close -but only hide Pool1Form
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Cancel = 1
Pool1Form.Hide
End Sub
'Close MainForm
Private Sub CLOSEROUTINE()
Dim frm As Form
For Each frm In Forms
Debug.Print frm.Name
Unload frm
Set frm = Nothing
Next frm
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call CLOSEROUTINE
End Sub
Re: How do you Declare Public Form and keep Data
The problem is that "QueryUnload" is called whenever an attempt is made to close the form (no matter how that attempt is made), so your code to unload the form also calls it.
To make the "hide" code only work when the user tries to close it, check the UnloadMode parameter like this:
VB Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
Cancel = True
Me.Hide '(you can always use "Me" instead of a form name inside the forms code)
End If
End Sub
Re: How do you Declare Public Form and keep Data
Thanks, that is exactly what I needed and the code works perfectly now, I did not want to disable control box because form will need to be open and closed many times with no loss of data as well as minimized. This is the right solution. I appreciate the help.