Test if the same form isLoaded.

M

Marshall Barton

TonyAntique said:
How can I test if the same form is already loaded?
The following in frmTest always returns true.

Private Sub Form_Open(Cancel As Integer)

If CurrentProject.AllForms("frmTest").IsLoaded = True Then
Form_frmTest.visible = True
Cancel = True
End If

End Sub

I want to stop a second existence of a form opening / loading, and making the
existing form visible if hidden.


I don't see how that can happen unless you go out of your
way to write code that can create multiple instances of a
specific form.

For sure, you can not open the same form twice using
OpenForm or from tha Database window. If you try to do
that, you will see that all OpenForm does is make it visible
and set the focus to the already open instance.

You also can not reliably use Form_frmTest syntax to
reference the already open form. That syntax refers to the
default instance of the form's class module and is primarily
useful along with the New keyword when using code to create
multiple instances of a form.

Bottom line is if you use OpenForm, you will not have the
situation you are concerned about.
 
T

TonyAntique via AccessMonster.com

How can I test if the same form is already loaded?
The following in frmTest always returns true.

Private Sub Form_Open(Cancel As Integer)

If CurrentProject.AllForms("frmTest").IsLoaded = True Then
Form_frmTest.visible = True
Cancel = True
End If

End Sub

I want to stop a second existence of a form opening / loading, and making the
existing form visible if hidden.
 
M

microsoft

Function isLoaded(strFormName) As Variant

Dim i As Integer

On Error GoTo Err_isLoaded

isLoaded = False
i = 0

While i < Forms.Count And isLoaded = False
Debug.Print i, Forms.Count
If Forms(i).Name = strFormName Then isLoaded = True
i = i + 1
Wend

Exit_isLoaded:
Exit Function

Err_isLoaded:
MsgBox Err.Description
Resume Exit_isLoaded

End Function
 
T

TonyAntique via AccessMonster.com

Thank you, but where do I put this code.

If I put it in Form_Open of Form_frmTest it always returns true if
strFormName = "frmTest".

Or am I missing Something?
Function isLoaded(strFormName) As Variant

Dim i As Integer

On Error GoTo Err_isLoaded

isLoaded = False
i = 0

While i < Forms.Count And isLoaded = False
Debug.Print i, Forms.Count
If Forms(i).Name = strFormName Then isLoaded = True
i = i + 1
Wend

Exit_isLoaded:
Exit Function

Err_isLoaded:
MsgBox Err.Description
Resume Exit_isLoaded

End Function
How can I test if the same form is already loaded?
The following in frmTest always returns true.
[quoted text clipped - 11 lines]
the
existing form visible if hidden.
 
T

TonyAntique via AccessMonster.com

Marshall
Thank you - I think I am trying to run before I can walk.

Marshall said:
How can I test if the same form is already loaded?
The following in frmTest always returns true.
[quoted text clipped - 10 lines]
I want to stop a second existence of a form opening / loading, and making the
existing form visible if hidden.

I don't see how that can happen unless you go out of your
way to write code that can create multiple instances of a
specific form.

For sure, you can not open the same form twice using
OpenForm or from tha Database window. If you try to do
that, you will see that all OpenForm does is make it visible
and set the focus to the already open instance.

You also can not reliably use Form_frmTest syntax to
reference the already open form. That syntax refers to the
default instance of the form's class module and is primarily
useful along with the New keyword when using code to create
multiple instances of a form.

Bottom line is if you use OpenForm, you will not have the
situation you are concerned about.
 
M

microsoft

The function tests if a given form is open. Code within a form cannot be
used to test if that same form is loaded. Think of it as you sitting at your
desk at work, picking up your work telephone and calling the same number to
see if you're at work. The fact that you picked up the phone to begin with
tells you that you are at work. The fact that you're at your desk to begin
with answers the question.

If a form is loaded, its loaded. It can't be loaded more than twice. The
code below tests to see if another form is open, for situations where you're
working with one form but need to return the user to a different form
entirely.

TonyAntique via AccessMonster.com said:
Thank you, but where do I put this code.

If I put it in Form_Open of Form_frmTest it always returns true if
strFormName = "frmTest".

Or am I missing Something?
Function isLoaded(strFormName) As Variant

Dim i As Integer

On Error GoTo Err_isLoaded

isLoaded = False
i = 0

While i < Forms.Count And isLoaded = False
Debug.Print i, Forms.Count
If Forms(i).Name = strFormName Then isLoaded = True
i = i + 1
Wend

Exit_isLoaded:
Exit Function

Err_isLoaded:
MsgBox Err.Description
Resume Exit_isLoaded

End Function
How can I test if the same form is already loaded?
The following in frmTest always returns true.
[quoted text clipped - 11 lines]
the
existing form visible if hidden.
 
T

TonyAntique via AccessMonster.com

Thank you for your detailed explanation.
It does explain well what I am doing wrong.
The function tests if a given form is open. Code within a form cannot be
used to test if that same form is loaded. Think of it as you sitting at your
desk at work, picking up your work telephone and calling the same number to
see if you're at work. The fact that you picked up the phone to begin with
tells you that you are at work. The fact that you're at your desk to begin
with answers the question.

If a form is loaded, its loaded. It can't be loaded more than twice. The
code below tests to see if another form is open, for situations where you're
working with one form but need to return the user to a different form
entirely.
Thank you, but where do I put this code.
[quoted text clipped - 32 lines]
 
J

JimBurke via AccessMonster.com

You may already have the info you need, but it's been my experience that if
you want to see if a form is already open, you would do it before you try to
open the form. i.e. if you have in your code:

DoCmd.OpenForm "frmName"....

then if you want to see if that form is already open, you would put an If
statement right before that:

If Not CurrentProject.AllForms("frmTest").IsLoaded Then
DoCmd.OpenForm "frmName"....

Maybe I'm misinterpreting your intentions.
 
T

TonyAntique via AccessMonster.com

Jim
Many thanks I will follow your advice - I was trying to be too clever.
You may already have the info you need, but it's been my experience that if
you want to see if a form is already open, you would do it before you try to
open the form. i.e. if you have in your code:

DoCmd.OpenForm "frmName"....

then if you want to see if that form is already open, you would put an If
statement right before that:

If Not CurrentProject.AllForms("frmTest").IsLoaded Then
DoCmd.OpenForm "frmName"....

Maybe I'm misinterpreting your intentions.
How can I test if the same form is already loaded?
The following in frmTest always returns true.
[quoted text clipped - 10 lines]
I want to stop a second existence of a form opening / loading, and making the
existing form visible if hidden.
 

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