channell said:
Hi,
I have a button on a form that opens a child form. I want to be able to
close the child form by clicking the same button.
I have it so it opens, but I can't get it to close. Thanks.
Right now, you may have code along these lines:
Private Sub cmdOpenClose_Click()
DoCmd.OpenForm "YourFormName"
End Sub
Change that so it reads:
Private Sub cmdOpenClose_Click()
If CurrentProject.AllForms("YourFormName").IsLoaded Then
DoCmd.Close acForm, "YourFormName", acSaveNo
Else
DoCmd.OpenForm "YourFormName"
End If
End Sub
Note that this doesn't do anything about the caption of the command button,
which you might prefer to say something like "Close Form" when the form is
currently open, and "Open Form" when the form is currently closed. You
could add code to change the button caption, but you would also to deal with
the situation where the user closed the form by some other method, not via
this command button. It gets much more complicated if you want to have the
command button's caption always reflect the current state of the "child"
form.