Check if Form is open (1 Viewer)

jayke

Registered User.
Local time
Today, 16:17
Joined
Nov 19, 2003
Messages
29
Hi there,

Is it possible to check with VBA whether a Form is open/loaded?
I am currently writing some VBA code behind a form (Form_1). In that code I want to check whether another form (Form_2) is open or loaded.

Can somebody help me? I think it is something stupid but I just cannot find it.

thx
 

yippie_ky_yay

Registered User.
Local time
Today, 11:17
Joined
Jul 30, 2002
Messages
338
Hey jayke,

here ya go:
Code:
If CurrentProject.AllForms("frmMyForm").IsLoaded Then
-Sean
 

jayke

Registered User.
Local time
Today, 16:17
Joined
Nov 19, 2003
Messages
29
Hi Sean,

Thanx for your reply.
I emmediatly tried it but I got following error:

-----
Error 2467 during execution:
The expression points to an object that is closed or that doesn't exist
-----

Because I got the error in dutch I transelated it. Maybe it looks a little wierd!

I used the following:
Code:
If CurrentProject.AllForms("Form_VestigingenForm").IsLoaded Then
     Form_VestigingenForm.opvullen_postcode
End If

Any idea?
 

RoyVidar

Registered User.
Local time
Today, 17:17
Joined
Sep 25, 2000
Messages
805
That method would return true if the form is open in design view. Is that what happened?

If that will become an issue also in production, then if you have the Northwind sample database (comes with Access), there's an IsLoaded function there, which will only return true if the form is open in one of the "display views".
 

RoyVidar

Registered User.
Local time
Today, 17:17
Joined
Sep 25, 2000
Messages
805
BTW - What is the form name (as viewed in the database window)?

Here you are testing for a form with the name "Form_VestigingenForm", afterwards you are trying to address a form (is this a public function or sub declared within the form, or should you have assigned some value?) then you named "VestigingenForm" with the "Form_" prefix, which is seldom used, unless instantiating a form (multiple instances).

If the form is named "VestigingenForm", with yippie_ky_yay's syntax

If CurrentProject.AllForms("VestigingenForm").IsLoaded Then
Forms!VestigingenForm.opvullen_postcode ' and some assigning?
End If

If the name of the form is "Form_VestigingenForm", I'd suggest a different naming convention, this is probably coming backt to bite you. But perhaps something like this would work?

If CurrentProject.AllForms("Form_VestigingenForm").IsLoaded Then
Forms![Form_VestigingenForm].opvullen_postcode ' and some assigning?
End If

I've included brackets in case of naming conflict, and the Forms keyword to refer to that instance of the forms collection
 

Mile-O

Back once again...
Local time
Today, 16:17
Joined
Dec 10, 2002
Messages
11,316
There's a function copyable from the Northwind database that does this in the Utility Functions module.
 

yippie_ky_yay

Registered User.
Local time
Today, 11:17
Joined
Jul 30, 2002
Messages
338
jayke said:
Hi Sean,

Thanx for your reply.
I emmediatly tried it but I got following error:

-----
Error 2467 during execution:
The expression points to an object that is closed or that doesn't exist
-----

Because I got the error in dutch I transelated it. Maybe it looks a little wierd!

I used the following:
Code:
If CurrentProject.AllForms("Form_VestigingenForm").IsLoaded Then
     Form_VestigingenForm.opvullen_postcode
End If

Any idea?
Can't be sure, but try this (removing "Form_"):
Code:
If CurrentProject.AllForms("VestigingenForm").IsLoaded Then
     VestigingenForm.opvullen_postcode
End If
-Sean
 

ghudson

Registered User.
Local time
Today, 11:17
Joined
Jun 8, 2002
Messages
6,195
The IsLoaded property is new to Access 2003. Previous versions can use the IsLoaded function which is included with the Northwind db as Milo stated.
 

jayke

Registered User.
Local time
Today, 16:17
Joined
Nov 19, 2003
Messages
29
Thx all for your responses.

I will try this later at home!

About the name of the forms. Well "Form_VestigingenForm" is the name of the form and now you mention it, it is not a nice naming convention. I will remove the prefix.

thx
 

Mile-O

Back once again...
Local time
Today, 16:17
Joined
Dec 10, 2002
Messages
11,316
Don't remove the prefix, just use the more common one:

frmVestigingenForm

No underscore as you'd probably have to surround the current form name with square brackets since the underscore is a "special" character.

And that function from Northwind is:

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

yippie_ky_yay

Registered User.
Local time
Today, 11:17
Joined
Jul 30, 2002
Messages
338
jayke said:
About the name of the forms. Well "Form_VestigingenForm" is the name of the form and now you mention it, it is not a nice naming convention. I will remove the prefix.
I only meant in your code because in the Project Explorer window it adds the prefix "Form_" even though you may have named your form without it (almost as if it has its own name for your forms) - this is probably why RoyVidar was asking. I believe that's why the quotes around the form name are needed in the code sample - so that it passes the name you gave it, rather than the one Access knows it as.

ghudson & Mile - I think the property may be older than 2003. I'm running Access 2002, and the IsLoaded Function in my Northwind db is slightly different than the one Mile-O posted and it uses the IsLoaded property in it:
Code:
Function IsLoaded(ByVal strFormName As String) As Boolean
 ' Returns True if the specified form is open in Form view or Datasheet view.
    Dim oAccessObject As AccessObject

    Set oAccessObject = CurrentProject.AllForms(strFormName)
    If oAccessObject.IsLoaded Then
        If oAccessObject.CurrentView <> acCurViewDesign Then
            IsLoaded = True
        End If
    End If
    
End Function
Now that I look at it more closely, it seems the only thing it adds is the check for design view (which I agree is most likely the problem). So Jayke, really this is the same thing as writing:
Code:
If CurrentProject.AllForms("frmVestigingenForm").IsLoaded And Forms!frmVestigingenForm.CurrentView <> acCurViewDesign Then
Using the Function is the better programming practice though! I often debate using them when I can just write it on one line (laziness:)), but really it's better if you use functions. And if you're using a version of Access that's at least older than 2002, you probably don't have a choice anyway.

-Sean
 

RoyVidar

Registered User.
Local time
Today, 17:17
Joined
Sep 25, 2000
Messages
805
This code:

If CurrentProject.AllForms("frmVestigingenForm").IsLoaded And Forms!frmVestigingenForm.CurrentView <> acCurViewDesign Then

Will probably give you an error if the form is not open, because it tests both conditions (also the view).

I think the .IsLoaded property, available thru the allforms collection, was included in the 2000 version.

For 2000, i don't think the acCurViewDesign constant is available, but test for 0 (as in the provided IsLoaded function).

I've seen others recommend against underscore too, not that I use it much in object names, but haven't seen any problems, except when referencing open forms using "Form_FormName" notation.
 

brickelldb

Registered User.
Local time
Today, 11:17
Joined
Mar 9, 2009
Messages
70
Sorry to open such an old thread but I am having a similar problem.

CurrentProject.AllForms("frmVestigingenForm").IsLo aded <----solved 1 of my problems, so I THANK YOU!!!

Now, is there an opposite to this? for "Is not loaded" ? I have it in a macro condition 1 for if it is already open and 1 for if it is not already open. I have tried isnotloaded but wouldnt compile. Anyone know what would be the appropriate syntax for if hte form is NOT loaded?
 

ghudson

Registered User.
Local time
Today, 11:17
Joined
Jun 8, 2002
Messages
6,195
Sorry to open such an old thread but I am having a similar problem.

CurrentProject.AllForms("frmVestigingenForm").IsLoaded <----solved 1 of my problems, so I THANK YOU!!!

Now, is there an opposite to this? for "Is not loaded" ? I have it in a macro condition 1 for if it is already open and 1 for if it is not already open. I have tried isnotloaded but wouldnt compile. Anyone know what would be the appropriate syntax for if hte form is NOT loaded?

Code:
If CurrentProject.AllForms("YourForm").IsLoaded = False Then
    MsgBox "Form is not loaded"
Else
    MsgBox "Form is loaded"
End If
OR

Code:
If CurrentProject.AllForms("YourForm").IsLoaded = True Then
    MsgBox "Form is loaded"
Else
    MsgBox "Form is not loaded"
End If
 

vbaInet

AWF VIP
Local time
Today, 16:17
Joined
Jan 22, 2010
Messages
26,374
Sorry to open such an old thread but I am having a similar problem.

CurrentProject.AllForms("frmVestigingenForm").IsLo aded <----solved 1 of my problems, so I THANK YOU!!!

Now, is there an opposite to this? for "Is not loaded" ? I have it in a macro condition 1 for if it is already open and 1 for if it is not already open. I have tried isnotloaded but wouldnt compile. Anyone know what would be the appropriate syntax for if hte form is NOT loaded?


Use this:

Sub Button1_Click(nameOfForm As String)
If Application.SysCmd(acSysCmdGetObjectState, acForm, nameOfForm) = acObjStateOpen Then
MsgBox "I am open"
End If
End Sub
 

brickelldb

Registered User.
Local time
Today, 11:17
Joined
Mar 9, 2009
Messages
70
that simple eh? just a "= false". lol thanks buddy, much appreciated!
 

Users who are viewing this thread

Top Bottom