by David Thompson - GUI Computing
It is sometimes handy to know if a user has made any changes to a dialog box between the time it is loaded and the time they press OK. For example, if the OK button updates a record in a remote database, then a lot of processing time can be saved if the form is simply unloaded. You cannot always rely on your users pressing Cancel in such situations.
One way to do this is to use a changed flag. Set the flag to false on Form_Load, and set it to true on the change events of all your controls.
This technique has a couple of problems:
The following two functions help overcome these difficulties.
The FormSave function saves the values of all your controls into a variant array. Call FormSave on the Load event of your form, after all your controls have been initialised.
The array varValues can be declared in the declarations section of your form.
Make sure the array is big enough for all your controls. 100 should be enough in most cases, unless your really keen on tab controls. Alternatively, you might want to size the array dynamically using ReDim. The FormChanged function compares the current values of your form with those stored in the variant array. It can be called on the click event of the OK button.
If FormChanged(Me, varValues()) Then ' do some processing End If Unload Me
If something has changed, it returns true and you'll have to go and do some other processing. Otherwise you can just unload the form.
There are a couple of things to be aware of when using these functions.
Firstly, there are some controls that won't work (eg. multi select list
box). Secondly, the functions have to be updated when a new custom control
is used. The advantage is that no new code has to be written when a
control is added to your form.