Results 1 to 5 of 5

Thread: [RESOLVED] How do you Declare Public Form and keep Data

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2005
    Posts
    29

    Resolved [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.

  2. #2
    Member
    Join Date
    Jan 2006
    Posts
    33

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2005
    Posts
    29

    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

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.  
    3.   If UnloadMode = vbFormControlMenu Then
    4.     Cancel = True
    5.     Me.Hide   '(you can always use "Me" instead of a form name inside the forms code)
    6.   End If
    7.  
    8. End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2005
    Posts
    29

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width