VB操作Excel无法更新的问题研究(续)
初步找到问题所在,是因为使用了form.hide而不是unload form。也就是隐藏窗体还是关闭窗体,导致了数据更新问题。
You can hide a form so that it is not visible to a user. When the form is hidden, the user cannot interact with the form, but you still have full programmatic control of them.
To hide a form
Use the Hide method.
For example, in the code associated with the Click event of a command button, you could include the following line of code:
THISFORM.Hide
When the user clicks the command button, the form remains in memory, but is not visible.
Releasing a Form
You can allow a user to release a form when he or she is finished interacting with it. When you release a form, you can no longer access properties and methods of the form.
To release a form
Call the Release method.
For example, in the code associated with the Click event of a command button, you could include the following line of code:
THISFORM.Release
When the user clicks the command button, the form closes.
Unload Forms
This will unload all forms in memory before the calling form is unloaded. Not only does this unload each form, it also fires each form's unload event, allowing for a more thorough clean up. Since this is placed in the Query_Unload event, errors can be handled and decisions can be made if the unloading should continue. This is especially useful when using other third-party controls such as the splitter control.
Place the following code in your main form's Query_Unload event.
Dim f As Form
For Each f In Forms
If f.hwnd <> Me.hwnd Then
Unload f
End If
Next