Slave form won't close from Master Close event

nschroeder

nschroeder
Local time
Yesterday, 19:36
Joined
Jan 8, 2007
Messages
186
I have a master form that opens a synchronized slave form in its Open event, and tries to close it in the Close event, but it won't close. It breezes past the Close command without an error, but nothing happens. The slave is a popup, non-modal form, but I've tried it both ways. Any suggestions? Here's the code.

Code:
Private Sub Form_Load()
    DoCmd.OpenForm "frmDetail"
End Sub

Private Sub Form_Current()
    Forms!frmDetail.Requery 
End Sub

Private Sub Form_Close()
    DoCmd.Close acForm, "frmDetail"
End Sub
 
This might be a good situation to use what's called a non-default instance of a form. In this case you create a new instance of the form object, assign it to a local variable, and set its visible property to true, so something like ...
Code:
[COLOR="Green"]'declare a variable of the specified type[/COLOR]
private m_frm as form_frmDetail

Private Sub Form_Load()
[COLOR="Green"]   'create a new instance[/COLOR]
   set m_frm = new form_frmDetail
[COLOR="Green"]   'and set the visible property[/COLOR]
   m_frm.visible = true
End Sub

Private Sub Form_Current()
[COLOR="Green"]   'requery the object[/COLOR]
   m_frm.requery
End Sub
...and notice there is no action required when you close the master form. The variable containing the slave form goes out of scope when the master closes and the object is destroyed automatically.
See if that works,
Mark
 
But I wonder why the original code won't work.

* Do you have any code in the slave form that could be preventing it from closing?
* Have you tried the code in the Unload event instead of the Close event?
 
I'm interested in seeing how this one is resolved because I can't think of anything.
 
I like lagbolt's response.

I've experienced the same issue. To answer the question why, I've found that code
Code:
CurrentProject.Forms("frmDetail").SetFocus
causes a run-time error if frmDetail is a Popup. Best I can figure is "DoCmd close" attempts to close the form that has the focus. But, as illustrated above, Popup forms do not receive the focus (nor do their Activate events fire... which unless I figure a solution soon will be the source of my first ? on this forum).

However, if I define an object variable for the form
Code:
Public objForm as Object
Set objForm as New Form_frmDetail
then
Code:
objForm.SetFocus
works as desired even if frmDetail is a popup.
 

Users who are viewing this thread

Back
Top Bottom