When [How?] to close a form..

jbfraleigh

Registered User.
Local time
Today, 17:46
Joined
Aug 8, 2000
Messages
22
I have a form that I want only to be opened from another form (i.e. the user presses a button from form1 and form2 is magically displayed).

My solution: In form2, I check to see if form1 is open in "use" mode. If not, I display a message and close form2.

The problem: The close code (docmd.close) is at the beginning of Form_Current. Apparently, Form_Current doesn't like to be interrupted. I receive a run-time error 2585 (This action can't be carried out while processing a form or report event).

Ok, so, umm, how exactly am I supposed to close the form? I tried the check during Form_Load instead, using the cancel variable instead of the docmd.close. However, I don't think this code got executed. It's almost like the form was already loaded or the Form_Load occurs after Form_Current (In my handy chart here, it says that Form_Current occurs when a form containing data is first opened and when you move to a different record.. so, I suppose it is possible).

Any thoughts?
 
You can check if the form is open using the code below and close it if true.

Code:
Function IsFormOpen(strFormName As String) As Boolean

[COLOR=SeaGreen]' Place function is a public module
' Comments : Determines if the named form is open
' Parameters: strFormName - form to check for
' Returns : True - form is open, False - form is not open[/COLOR]
IsFormOpen = (SysCmd(acSysCmdGetObjectState, acForm, strFormName) = acObjStateOpen)

End Function

Code:
    If IsFormOpen("frmA") = True Then
        DoCmd.Close acForm, "frmA"
    Else
        MsgBox "Form A is not open"
    End If
 
OK, I might be missing something, but if Form2 is opened from Form1 only, why do you check in Form2 a condition on Form1? Would not the code executed when the button is pressed to open Form2 on Form1 be a better place to check it and just not open Form2 if the conditions are not meet? :confused:
 
Fofa,

Let's say that some user tries to open Form2 without going through Form1. The user will never actually press a button on form1. They may try to open it directly from the "forms" tab. If this happens, I want to be able to gracefully close Form2.

Ghudson,

This is similar to code that I'm using -- However, I need to close frmB if frmA is not open. The problem occurs when I try to close frmB from frmB (see initial post in thread and my response to Fofa).
 

Users who are viewing this thread

Back
Top Bottom