PDA

View Full Version : docmd.close form



wasim_sono
06-24-2008, 05:21 AM
Dear All

I have a subform on a main form. Sub form has an exit button. When this sub form display on main form then if Exit button clicked then the main form close down. I wanty to close only sub form.

Thanks.

Wasim

Oorang
06-24-2008, 05:54 AM
If you call DoCmd.Close with no arguments, it will close down Screen.ActiveForm (which in this case will be the parent form). If you try to close the subform by name, it will prevent the parent form from being closed. But you cannot close a form when it is being used as a subform using the close method... So the close button will work when the form is being used as a form, but not when it is being used as a subform. What you can do is to close the form when it's being used as a form, and hide it when it is a subform. Which should work just fine, as closing the parent form will close the subform hidden or not.
Private Sub MyButton_Click()
Dim frmParent As Access.Form
On Error Resume Next
Set frmParent = Me.Parent
On Error GoTo Err_Hnd
If frmParent Is Nothing Then
DoCmd.Close acForm, Me.Name
Else
frmParent.Controls(0).SetFocus
Me.Visible = False
End If
Exit Sub
Err_Hnd:
MsgBox Err.Description
End Sub