View Full Version : Check if Form is open
jayke 11-24-2004, 10:35 AM 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 11-24-2004, 10:50 AM Hey jayke,
here ya go:
If CurrentProject.AllForms("frmMyForm").IsLoaded Then
-Sean
jayke 11-24-2004, 11:26 AM 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:
If CurrentProject.AllForms("Form_VestigingenForm").IsLoaded Then
Form_VestigingenForm.opvullen_postcode
End If
Any idea?
RoyVidar 11-24-2004, 11:34 AM 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 11-24-2004, 11:47 AM 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 11-24-2004, 11:53 AM There's a function copyable from the Northwind database that does this in the Utility Functions module.
yippie_ky_yay 11-24-2004, 12:05 PM 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:
If CurrentProject.AllForms("Form_VestigingenForm").IsLoaded Then
Form_VestigingenForm.opvullen_postcode
End If
Any idea?
Can't be sure, but try this (removing "Form_"): If CurrentProject.AllForms("VestigingenForm").IsLoaded Then
VestigingenForm.opvullen_postcode
End If
-Sean
ghudson 11-24-2004, 01:36 PM 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 11-25-2004, 01:26 AM 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 11-25-2004, 01:34 AM 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:
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 11-25-2004, 06:19 AM 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:
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 FunctionNow 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: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 11-25-2004, 12:49 PM 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.
|
|