Open Close Button

C

channell

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.

-Scott Channell
 
D

Dirk Goldgar

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.
 
G

Gina Whipp

CHannell,

You could use the IsLoaded function to detrmine if the form is open and if
it is close it...

Copy the below into a Module window, save and name modUtilities...

Function IsLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open in Form view or Datasheet
view.

Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <>
conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If

End Function


On/behind your button you would put...

If Not IsLoaded("YourFormName") Then
DoCmd.OpenForm "YourFormName"
Else
DoCmd.Close acForm, "YourFormName"
End If

This will only work if you are opening a seperate form and the form you are
opening is not a subform of the main form you opening.
--
Gina Whipp

"I feel I have been denied critical, need to know, information!" - Tremors
II

http://www.regina-whipp.com/index_files/TipList.htm
 
C

channell

Thanks a Million! I had an IfThen Statement, but I wasn't sure how to
articulate to vb about the form being loaded already. IsLoaded worked great!
Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top