Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I restart a program and reset the variables 1

Status
Not open for further replies.

vork

Programmer
Mar 24, 2004
6
CA
hello

I am creating a reset button and I want it to restart my entire program or simply reload the current form but it must also reinitialize my variables. The problem is I have static variables declared in a timer routine.

Any suggestions?
 
It's better to continue in your existing thread, rather than start a new thread for a question already answered. This appears to be a continuation of thread222-807504.

As I said in that post read faq222-2244 to see how the forum works.

For this question, look up the scope of Statics in VBHelp. That will clarify for you that <In form modules, static variables retain their value until the form is closed>

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
I know that static variables reset when the form is closed but is there a way to close the form, then reopen it and have the variables reset. I've tried refreshing the form, Call Form_Load and so on with no success. Is there a better way?
 
The first options that come to me are passing some sort of flag to the Timer sub:

Code:
private ResetTimer as Boolean

Timer procedure:
Code:
If ResetTimer then
    'static variables are zeroed
    ResetTimer = False
End If

Another option would be to run your program out of a Sub Main(), and use modular code to unload and then reload the form again.

Code:
Unload frmMain
frmMain.Show 'will reload it as well
 
Thanks Andy but It still didn't work. I don't understand because reloading the form should've reset the variables.

Any other ideas?
 
Unloading a form does not terminate it; you need to reduce the reference count to 0...
 
Why don't you initialise the variables in the form_load event so they are reset (to that default value) every time the form is loaded...
 
Two forms. Second form has a timer, a textbox and the following code:
Code:
Option Explicit

Private Sub Form_Load()
    Timer1.Enabled = True
    Timer1.Interval = 1000
End Sub

Private Sub Timer1_Timer()
    Static wombat
    
    Debug.Print wombat
    wombat = wombat + 1
    Text1.Text = wombat
End Sub
First form has two command buttons and the following code:
Code:
Option Explicit
Private myForm As Form

Private Sub Command1_Click()
    If myForm Is Nothing Then
        Set myForm = New Form2 ' new instance, and thus reference count, to Form2
    End If
        myForm.Show
End Sub

Private Sub Command2_Click()
    Unload myForm ' Note that this won't terminate the form
    Set myForm = Nothing ' This will because it reduces ref count to 0
    Set myForm = New Form2
    myForm.Show
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top