Referencing unopened forms

squinn03

squinn
Local time
Today, 04:01
Joined
Apr 19, 2007
Messages
16
The database I'm working on Has a form (FormA) which references another form (FormB) whether FormB is open or not.

This presents a minor problem because after the reference is made, access now thinks that FormB is open.

Here's some sample code:

Sub FormA_Load()

FormB.cbo1.setfocus
if isNull(FormB.cbo1) then
'do something crazy
else
'act normally
end if

end Sub
[/SIZE]


So the "setFocus" call is what causes Access to think that FormB is open, but I've found that the setfocus needs to be called in order for the If block to execute properly.

Does anyone have any suggestions on how I can solve this? I'm looking for an Access solution, such as an "isOpen" function that can be used.

The only thing I can think of is to add a global flag to set whether FormB is really open on not.

Thanks in advance.
 
The database I'm working on Has a form (FormA) which references another form (FormB) whether FormB is open or not.

This presents a minor problem because after the reference is made, access now thinks that FormB is open.

Here's some sample code:

Sub FormA_Load()

FormB.cbo1.setfocus
if isNull(FormB.cbo1) then
'do something crazy
else
'act normally
end if

end Sub
[/SIZE]


So the "setFocus" call is what causes Access to think that FormB is open, but I've found that the setfocus needs to be called in order for the If block to execute properly.

Does anyone have any suggestions on how I can solve this? I'm looking for an Access solution, such as an "isOpen" function that can be used.

The only thing I can think of is to add a global flag to set whether FormB is really open on not.

Thanks in advance.

Code:
syscmd(acSysCmdGetObjectState ,acForm,"FormNameHere")

This function returns a zero if the form is closed and a 1 if the form is open.
 
That command actually returns 1 either way in my case. I guess Access really thinks that the form is open, even though it has only been referenced.


Any other ideas?
 
I'm going to use
If Form.Visible = False then
'close the form
end if

Thanks for the suggestion
 
So the "setFocus" call is what causes Access to think that FormB is open, but I've found that the setfocus needs to be called in order for the If block to execute properly.
Incorrect, if the form is not opened then the setfocus should generate an error saying it can't find the form. Your If should not be dependent upon the focus being set on the item. It could be that your reference to your form is not correct -

It really should be this (from another form):

Code:
Private Sub FormA_Load()

If IsLoaded(Forms!FormB) Then
   If isNull(Forms!FormB.cbo1) then
      ' do something crazy
   Else
      'act normally
   End if
End If

End Sub
 
What is the "IsLoaded" function you're all talking about? Isn't that a UDF that one would need to copy paste into a module? Here's one implementation that should work
Code:
Public Function IsLoaded(ByVal strForm As String) As Boolean
    Const conObjStateClosed = 0
    Const conDesignView = 0
    ' Checks whether a form is open, and with a view
    ' different from design. Usage:
    ' debug.print IsLoaded("NameOfForm")

    If SysCmd(acSysCmdGetObjectState, acForm, strForm) <> _
                             conObjStateClosed Then
        If Forms(strForm).CurrentView <> conDesignView Then
            IsLoaded = True
        End If
    End If

End Function
and I would very much like to see the real code, not the thing pasted in here, as what I believe might be the cause of this, is using a reference like this

Form_NameOfForm.SomeControl.SomeMethod

which has a tendency of working regardless of whether the form is open or not, and if it isn't open, it'll create an instance of it. When I'm not intending to instantiate a form, I'll reference through the forms collection

Forms("NameOfForm").SomeControl.SomeMethod
 
Forms("NameOfForm").SomeControl.SomeMethod

Forms!NameOfForm.SomeControl.SomeMethod is the same as above. The Forms! part is utilizing the Forms Collection (Pat Hartman has posted many times around this). So, either using Forms!NameOfForm or Forms("NameOfForm) is actually personal preference except that when you need to use a variable for the form name you must use Forms(YourVariableHere) to reference it.
 
Form_NameOfForm.SomeControl.SomeMethod

is different from

Forms("NameOfForm").SomeControl.SomeMethod

The first one might instantiate a form, the second is one way referencing through the forms collection.
 
And I didn't say that wasn't, but I said that Forms!NameOfForm is the same as Forms("NameOfForm") and it will not instantiate an object. I would never use Form_NameOfForm in code anyway and if I came across as saying I would I apologize.
 
Oh, and I found this, which I THOUGHT was possible without the IsLoaded function that Roy listed:

Code:
If CurrentProject.AllForms("YourFormName").IsLoaded Then

You can use that to test to see if a form is loaded without having to create a module and paste the function in.
 
I believe the
Code:
If CurrentProject.AllForms("YourFormName").IsLoaded Then
...is only available in ac2003+.
 
I believe the
Code:
If CurrentProject.AllForms("YourFormName").IsLoaded Then
...is only available in ac2003+.

Gotta disagree as I just tested it in A2K and it worked great for me. Apparently it works for A2K and above.
 

Users who are viewing this thread

Back
Top Bottom