Check for open forms? (1 Viewer)

bulrush

Registered User.
Local time
Today, 11:38
Joined
Sep 1, 2009
Messages
209
A2003 on WinXP

I have several tables: table1 which is linked to multiple entries in table2, each record of which is linked to table3. Therefore, I have forms like: Form1 (with subform) which links to Form2 (with subform) which links to Form3.

So, if the user opens Form3, I need to make sure Forms1 and Forms2 are open first. IF they are not open, Form3 needs to display an error message, then close.

- Which event in Form3 do I use to check for the other forms? Form_Open, Form_Activate? Something else?

- What code do I use to see if a form is open?

Thanks.
 
Last edited:

jdraw

Super Moderator
Staff member
Local time
Today, 11:38
Joined
Jan 23, 2006
Messages
15,404
Here's a function that should tell if a specified Form is open. It expects the name of the form as input, and returns True or False


Function IsLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open
Dim oAccessObject As AccessObject

Set oAccessObject = CurrentProject.AllForms(strFormName)
If oAccessObject.IsLoaded Then
IsLoaded = True
else
IsLoaded = False
End If
End Function
 

boblarson

Smeghead
Local time
Today, 08:38
Joined
Jan 12, 2001
Messages
32,059
Here's a function that should tell if a specified Form is open. It expects the name of the form as input, and returns True or False

I don't get why one would use this function instead of just using the direct method that is even found in that function:

Code:
If CurrentProject.AllForms(strFormName).IsLoaded = True Then
 

jdraw

Super Moderator
Staff member
Local time
Today, 11:38
Joined
Jan 23, 2006
Messages
15,404
Good point Bob. I looked for a check for IsLoaded and found a function and passed it on. But as you say, no reason not to use a direct approach.
 

bulrush

Registered User.
Local time
Today, 11:38
Joined
Sep 1, 2009
Messages
209
Bob,
They create a new function for modularity. Every once in a while, Access stops supporting a function, property, or method, and if you have to search 57 modules to update your direct reference, it takes a lot of time. Or you could make a function, change it once, and you're done.

So, I should use IsLoaded in my Form3_Open event?
Code:
Private Sub Form3_Open(Cancel As Integer)
If IsLoaded("Form1") = False Then
    MsgBox "ERROR: Form Form1 is not loaded."
    Cancel = True ' Hopefully this will close this form
End If
Will my code close the current form? Or do I have to do:
docmd.close
 

bulrush

Registered User.
Local time
Today, 11:38
Joined
Sep 1, 2009
Messages
209
I'm getting an error in this line:
Set oAccessObject = CurrentProject.AllForms(strFormName) ' ERROR

The error is: "This object refers to an object that is not open."

I put this code in Form2_Open event, and made sure Form1 was NOT open, and got this error. Any ideas?

Also, this is for A2003.
 

boblarson

Smeghead
Local time
Today, 08:38
Joined
Jan 12, 2001
Messages
32,059
Modularity is fine but for this I don't think it is necessary. But if you want to use it, then I would not use the Access object as that is overkill:
EDIT: Actually it can be edited down to a single line as well
Code:
Function IsLoaded(ByVal strFormName As String) As Boolean
    IsLoaded = CurrentProject.AllForms(strFormName).IsLoaded 
End Function
 
Last edited:

ChrisO

Registered User.
Local time
Tomorrow, 01:38
Joined
Apr 30, 2003
Messages
3,202
Bulrush.

This line in post #1:-
"So, if the user opens Form3, I need to make sure Forms1 and Forms2 are open first. IF they are not open, Form3 needs to display an error message, then close."

That would seem to imply that Form3 would never be opened 'stand alone'; so why let the user open it?

Chris.
 

boblarson

Smeghead
Local time
Today, 08:38
Joined
Jan 12, 2001
Messages
32,059
Bulrush.

This line in post #1:-
"So, if the user opens Form3, I need to make sure Forms1 and Forms2 are open first. IF they are not open, Form3 needs to display an error message, then close."

That would seem to imply that Form3 would never be opened 'stand alone'; so why let the user open it?

Chris.

Good point.
 

smig

Registered User.
Local time
Today, 18:38
Joined
Nov 25, 2009
Messages
2,209
I want to check for an open form, and close it in case it's open

isn't it better using this:
Code:
Sub CloseOpenForm(ByVal strFormName As String)
    on error resume next
    Docmd.Close acForm, strFormName
End Sub

rather then this:
Code:
Sub CloseOpenForm(ByVal strFormName As String)
    if CurrentProject.AllForms(strFormName).IsLoaded = true then
        Docmd.Close acForm, strFormName
    end if
End Sub

only to prevent the need of checking all forms in project, or is not checking all forms ?
 

boblarson

Smeghead
Local time
Today, 08:38
Joined
Jan 12, 2001
Messages
32,059
I want to check for an open form, and close it in case it's open

isn't it better using this:
Code:
Sub CloseOpenForm(ByVal strFormName As String)
    on error resume next
    Docmd.Close acForm, strFormName
End Sub

rather then this:
Code:
Sub CloseOpenForm(ByVal strFormName As String)
    if CurrentProject.AllForms(strFormName).IsLoaded = true then
        Docmd.Close acForm, strFormName
    end if
End Sub

only to prevent the need of checking all forms in project, or is not checking all forms ?
Using the second method isn't going to be checking all forms in the project. It will only check the one you are passing the name for. But you can use the one with the error handler just fine. I would add the acSaveNo at the end though just to keep from giving the user the opportunity to save any design changes that may have occurred (filters, etc.) because acSavePrompt is the default.
 
Last edited:

Users who are viewing this thread

Top Bottom