Refresh Data after closing form
from my form, i can call another form using 1 CommandButton. However, i want to refresh the data in the first form after i close the other form. The problem is that i cannot modify the second form, so i need to know whether there is any other way to know that the second form has been closed ?
maybe i have to add some line of code after i call the second form ? now i only use like this ::
sub CommandButton_Click()
Form2.Initialize
End Sub
[464 byte] By [
Dock] at [2007-11-20 11:48:10]

# 1 Re: Refresh Data after closing form
How many forms are you talking about here, 2 or 3.
If 2 then when Form2 closes, its gone. Calling Form2 initialize probably isn't going to help, but it will load Form2 again and not show it.
If Form2 shouldn't be closed, then don't allow the user to close it, make it so you can only close it thru code.
Provide more specific details. What is on Form2, after it is closed, that needs to be accessed? Which form(s) are loading Form2? Is Form2 modal or not?
# 2 Re: Refresh Data after closing form
If you want the info reseted after the program is closed you have two options:
*Registry, Not recommended, make values of what ever you want to restart to, and read those when the program loads.
*FormLoad fixing, much easier but might not work to all.
When user closes the application, in order to get back to it, or the info on it, he will have to REOPEN the form, meaning it will call the form_load() and because of that you can reset all the fields you want in FormLoad, that way, when ever the user opens the form again, it is reseted.
Morhsn at 2007-11-9 19:33:20 >

# 3 Re: Refresh Data after closing form
I am assuming that you can modify code for form2:
Create one class and in it declare withevents object of form2.
Here you will get the unload event for form2, in it set the values for form1.
# 4 Re: Refresh Data after closing form
Couple ways to do this. Add a custom property (or properties) to the form for the data you need to retrieve. Then, instead of calling it from your CommandButton like this:
Sub CommandButton_Click()
Form2.Initialize
End Sub
Call it like this and retrieve it's return data:
'In Form2
'Declarations
Private fsCustom As String
Public Property Get CustomProperty() As String 'Or whatever you need to return.
CustomProperty = fsCustom
End Property
Private Sub TheOneThatExitsTheForm()
fsCustom = WhatEver
Me.Hide
End Sub
'In Form1
Sub CommandButton_Click()
Dim fForm As Form2
Set fForm = New Form2
Call fForm.Show(vbModal) 'Passes control to Form2
'Control passes to Form2
'Form2 closes itself with Me.Hide, and execution returns here.
Something = fForm.CustomProperty 'Pass the return values back.
Unload fForm 'Done with the form, so unload it.
End Sub
Another option is to pass a reference of the calling form to the sub-form, again through a custom property. Just make sure to release it when Form2 closes. Note: this one will work on non-modal forms, but be VERY careful not to create situations where you have circular references or hanging references to the calling form after it closes.
'In Form2.
'Declarations:
Private fCaller As Form1
Private Property Set CallingForm(oForm As Form1)
Set fCaller = oForm
End Property
Private Sub Form_Unload(Cancel As Integer)
Set fCaller = Nothing
End Sub
'In Form1
Sub CommandButton_Click()
Load Form2
Set Form2.CallingForm = Me
Form2.Show
End Sub
NOTE: I use the first method all the time to pass classes between different forms. If you encapsulate your data into objects, this method becomes very powerful.