Code dependant on if a form is open or closed

M

Mark Senibaldi

Hi,
I have 2 forms (frm_1 & frm_2). After I click on a command button I want
frm_1 to close ONLY IF frm_2 is open. If frm_2 is not open then I want frm_1
not to close. What code could I use to accomplish this?
 
D

Damon Heron

Use the isloaded function in the click event:

If IsLoaded("Your second form") Then
docmd.close
else
' add a msgbox stating the form is not open
Cancel = True
end if

Here is the function - put in module

Function IsLoaded(ByVal strFormName As String) As Integer
' 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

HTH
Damon
 
M

Mark Senibaldi

Thanks. Does this code have to be in a function in a module or can it be in
the command button?
 
D

Damon Heron

The first part is in your command button click event that you mentioned in
your first post. The function "IsLoaded" is best put in
a module, so it can be used thruout the project, because a function in the
form's code will only be usable by the form.

Damon
 
Top