zhuhail
asked on
Check anything changed
Any good idea:
An application (VB5) holds some data. Users may change them in run time.
I want to check if anything is changed when the users exit the application.
I don't like to put every thing in memory when uers start the application.......
Thank you in advance.
An application (VB5) holds some data. Users may change them in run time.
I want to check if anything is changed when the users exit the application.
I don't like to put every thing in memory when uers start the application.......
Thank you in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The following assumes that your data is loaded into
text boxes, masked ed boxes or combo boxes in a form
called frmMyForm.
Put this code in the click event for a close / exit button
on the form. It will set blnSomethingChanged to True
if the user has changed something in one of the fields.
Dim objLocalObject As Object
Dim blnSomethingChanged As Boolean
blnSomethingChanged = False
For Each objLocalObject In frmMyForm
If (TypeOf objLocalObject Is TextBox) Or (TypeOf _ objLocalObject Is MaskEdBox) Or (TypeOf objLocalObject Is DBCombo) Then
If objLocalObject.DataChanged Then
blnSomethingChanged = True
End If ' LocalObject.DataChanged
End If ' TypeOf LocalObject
Next
If blnSomethingChanged Then
' code whatever you want
' to happen if some data has changed here
End If
text boxes, masked ed boxes or combo boxes in a form
called frmMyForm.
Put this code in the click event for a close / exit button
on the form. It will set blnSomethingChanged to True
if the user has changed something in one of the fields.
Dim objLocalObject As Object
Dim blnSomethingChanged As Boolean
blnSomethingChanged = False
For Each objLocalObject In frmMyForm
If (TypeOf objLocalObject Is TextBox) Or (TypeOf _ objLocalObject Is MaskEdBox) Or (TypeOf objLocalObject Is DBCombo) Then
If objLocalObject.DataChanged
blnSomethingChanged = True
End If ' LocalObject.DataChanged
End If ' TypeOf LocalObject
Next
If blnSomethingChanged Then
' code whatever you want
' to happen if some data has changed here
End If
I like the way NeuralNick determines if the value in the controls are changed. I would not put the code in the click event of a command button though. A better option would be to put it in the forms QUERYUNLOAD event. That way if the user closes the form using something other then then command button...ie the X button on the form, it will still be executed.
Steven
Steven
ASKER
Hi experts
Thank you very much for your answers and comments.
I like the ideas:
1. .ini file (deighton)
2. DataChanged property (NeuralNick)
3. QUERYUNLOAD event (sdbanks)
and in my case, I will use all of the above ideas!
BTW, the END statement may not fire the QueryUnLoad event although END causes the form unloading. Strange?
Thank you very much for your answers and comments.
I like the ideas:
1. .ini file (deighton)
2. DataChanged property (NeuralNick)
3. QUERYUNLOAD event (sdbanks)
and in my case, I will use all of the above ideas!
BTW, the END statement may not fire the QueryUnLoad event although END causes the form unloading. Strange?
With End try making a simple form with two command buttons. button1 is unload me, button2 is End.
Put a msgbox in your unload code. button1 displays the msgbox, button2 doesn't.
End is an immediate end to the program. does close files I believe.
Put a msgbox in your unload code. button1 displays the msgbox, button2 doesn't.
End is an immediate end to the program. does close files I believe.
ASKER
Hi deighton
I beleive that the test you suggested will be correct.
Do you try to show me that END doesn't fire QueryUnLoad event? However, END should do unloading the form before ending the program....am I write? If END does unloading the form, why not fire QueryUnLoad event?
Cheers
I beleive that the test you suggested will be correct.
Do you try to show me that END doesn't fire QueryUnLoad event? However, END should do unloading the form before ending the program....am I write? If END does unloading the form, why not fire QueryUnLoad event?
Cheers
End stops the program running immediately, no more code is executed. If you use an End the program will finish and the forms will be unloaded, but you won't get any queryunload, terminate or unload event code run.
Some programmers oppose the use of the end command in VB. It is a legacy of the original implementation of BASIC.
Some programmers oppose the use of the end command in VB. It is a legacy of the original implementation of BASIC.
I do not use END anymore as it does not clean up memoryin vbs COM environment. I now use the following routine:
For i = forms.count -1 to 0 step -1
unload forms(i)
Next
end
For i = forms.count -1 to 0 step -1
unload forms(i)
Next
end
ASKER
Many thanks to deighton.
To sdbanks:
I will increase the pointe since the answers are more than I expected.
Your END routine is quite interesting. Can you tell me
1. why not use
For i = 0 to forms.count-1
2. How to close whole program if it includes non-form modules?
Cheers.
> For i = forms.count -1 to 0 step -1
> unload forms(i)
> Next
> end
To sdbanks:
I will increase the pointe since the answers are more than I expected.
Your END routine is quite interesting. Can you tell me
1. why not use
For i = 0 to forms.count-1
2. How to close whole program if it includes non-form modules?
Cheers.
> For i = forms.count -1 to 0 step -1
> unload forms(i)
> Next
> end
In answer to below, no reason not to use yours as far as I can see, I just started using the other code as I found it on the Net.
> 1. why not use
> For i = 0 to forms.count-1
>
In answer to your second question re:
> 2. How to close whole program if it includes non-form modules?
I never thought about it before. What I try to do when I am coding is to make sure that anything that I use a Set command for, ie..
Set m_client = new cClient
I set that variable to nothing at some point. My personal preference has been:
1. If I Set a variable in a sub-routine, I try to set it to nothing as soon as it is no longer used, usually at the end of a routine.
2. If a SET statement is used in a class_initialize of a class module, I set it to nothing in the class_terminate event.
3. If a Set variable is used when a form is loaded, I try to set it to nothing when the form unloads though the forms query_unload event.
4. I have not been setting local variable and public varaibles to nothing as I believe they do not need this special treatment.
Hope this helps. Any comments welcome.
Steven
> 1. why not use
> For i = 0 to forms.count-1
>
In answer to your second question re:
> 2. How to close whole program if it includes non-form modules?
I never thought about it before. What I try to do when I am coding is to make sure that anything that I use a Set command for, ie..
Set m_client = new cClient
I set that variable to nothing at some point. My personal preference has been:
1. If I Set a variable in a sub-routine, I try to set it to nothing as soon as it is no longer used, usually at the end of a routine.
2. If a SET statement is used in a class_initialize of a class module, I set it to nothing in the class_terminate event.
3. If a Set variable is used when a form is loaded, I try to set it to nothing when the form unloads though the forms query_unload event.
4. I have not been setting local variable and public varaibles to nothing as I believe they do not need this special treatment.
Hope this helps. Any comments welcome.
Steven
ASKER
Hi Steven
Sorry, I didn't make clear for my last quastion.
1. You said you don't use END statement.
2. If I don't like the user to click the very small cross button.
3. I need to set a nice button and (in its click event) code something to stop the running of the program.
4. I may not stop the program by only unloading the forms...especially, if there are some class moduls and standard moduls.
So what to do to stop the program without END?
Cheers.
Sorry, I didn't make clear for my last quastion.
1. You said you don't use END statement.
2. If I don't like the user to click the very small cross button.
3. I need to set a nice button and (in its click event) code something to stop the running of the program.
4. I may not stop the program by only unloading the forms...especially, if there are some class moduls and standard moduls.
So what to do to stop the program without END?
Cheers.
> 1. You said you don't use END statement.
I should have said that I do not use end statement in forms to stop by itself, at the end of my sample close routine, I have an end statement after the forms are unloaded.
> 2. If I don't like the user to click the very small cross button.
> 3. I need to set a nice button and (in its click event) code something to stop the
> running of the program.
This method does not prevent you from providing the end user with a button or menu option to exit the form or program. As an example, You provide a quit program button in the main form in your program. Rather the quiting the program with end, you put the code "unload me" in the buttons click event. This will cause the query_unload event on the form to run which does the actual cleanup for the program.
> 4. I may not stop the program by only unloading the forms...especially, if there
> are some class moduls and standard moduls.
>
> So what to do to stop the program without END?
There is nothing to stop you from putting the SET xxx = Nothing in the a forms query unload event also. If you do not have any forms, you should still have an exit routine that will do the clean-up of memory. Then you could use the end statement at the end of it.
ASKER
I think you have made the things much more clear and I have increased the point.
Did you said, when ending a program
1. you never assign variables (from Dim) to nothing, even the very big arrays;
2. you always set the objects (from Set...New..) to nothing
Cheers.
Did you said, when ending a program
1. you never assign variables (from Dim) to nothing, even the very big arrays;
2. you always set the objects (from Set...New..) to nothing
Cheers.
I am not sure about the dim staement part of your response. You might want to check documentation to see if they should be Set to nothing when they are no longer used.
Steven
Steven
You could write the contents of each control to a text file using write #1. create the text file in your temp directory. When the user exits you could retrieve the values from the file using read #1, etc and compare the new values to th old?