Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close a form if it is open

Status
Not open for further replies.

neemi

Programmer
May 14, 2002
519
0
0
GB
Access 2002

Does anyone know how I can close a form if it is open?

I am opening another form and when this is opened i want certain forms not be open as the code will use this same form. So if the form is open it will be closed. If not open then it will do nothing.

eg
If form2 is open then
close form2
else
' do nothing
end if

cheers in advance
neemi
 
You need to test if the form in question is open and then close it if true. Here is a simple way to do that.

Function IsFormOpen(strFormName As String) As Boolean
IsFormOpen = (SysCmd(acSysCmdGetObjectState, acForm, strFormName) = acObjStateOpen)
End Function

Private Sub Command1_Click()
If IsFormOpen("frmA") = True Then
MsgBox "Form A is open"
DoCmd.Close acForm, "frmA"
Else
MsgBox "Form A is not open"
End If
End Sub
 
Cheers for your reply, but once i posted the question online I tested to see what would happen if I just used

docmd.close acform, "formName"

and it works fine. It doesn't error if the form is not already open, which i was worried might happen.

Cheers,

neemi
 
I always use this command with the sub that opens another form...

DoCmd.Close acForm, Me.Name

That will "generically" close the open from that is used to open another form.

HTH
 
Hi neemi,

You could just close it anyway and ignore any errors ..

Code:
On Error Resume NEXT
DoCmd.Close acform "Form2"
On Error Goto 0

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top