Check if Form is open

19 years if you include the OP.
I am sure that question has been asked recently as well?
Well a variation of the question?

 
Use this:

Sub Button1_Click(nameOfForm As String)
If Application.SysCmd(acSysCmdGetObjectState, acForm, nameOfForm) = acObjStateOpen Then
MsgBox "I am open"
End If
End Sub
Just a little correction, to be more specific:

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

Should be "NameOfForm" in quotes.
 
Should be "NameOfForm" in quotes
No. The name of the form is being passed to the sub as a variable, so there should be no quotes.

In addition,
Code:
SysCmd(acSysCmdGetObjectState, acForm, "SomeForm")
might return four different values, 0, 1, 2, 4, (corresponding to 0, acObjStateOpen, acObjStateDirty, and acObjStateNew), so the posted code will fail if the form is open, but has a dirty or a new record.

A complete function to return whether a form is open AND not in design view would coerce the SysCmd() result to a boolean, and then check Form.CurrentView, like...
Code:
Function IsLoaded(Name As String) As Boolean
    If SysCmd(acSysCmdGetObjectState, acForm, Name) Then
        IsLoaded = Not Forms(Name).CurrentView = acViewDesign
    End If
End Function
 
No. The name of the form is being passed to the sub as a variable, so there should be no quotes.

In addition,
Code:
SysCmd(acSysCmdGetObjectState, acForm, "SomeForm")
might return four different values, 0, 1, 2, 4, (corresponding to 0, acObjStateOpen, acObjStateDirty, and acObjStateNew), so the posted code will fail if the form is open, but has a dirty or a new record.

A complete function to return whether a form is open AND not in design view would coerce the SysCmd() result to a boolean, and then check Form.CurrentView, like...
Code:
Function IsLoaded(Name As String) As Boolean
    If SysCmd(acSysCmdGetObjectState, acForm, Name) Then
        IsLoaded = Not Forms(Name).CurrentView = acViewDesign
    End If
End Function
Absolut true.. Sorry I forgot the original code was a SUB, I am actually not using it as a SUB, hence my code differed, but yes, my mistake I stand corrected, I am only using this part:
Code:
If Application.SysCmd(acSysCmdGetObjectState, acForm, "NameOfForm") = acObjStateOpen Then
    MsgBox "I am open"
End If

Hence the reason, I forgot I was applying my code in a different way.
 
Just beware of the fact that this expression...
Code:
SysCmd(acSysCmdGetObjectState, acForm, "NameOfForm") = acObjStateOpen
...will return False if the form is open, but contains an unsaved record.

: )
 
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
Code:
Dim ActiveForm As AccessObject
For Each ActiveForm In CurrentProject.AllForms
    With ActiveForm
        If ActiveForm.Name = "Form_2" And ActiveForm.IsLoaded Then
            MsgBox .Name
        End If
    End With
Next
 
@LarryE ,

Why bother looping through every form in the .AllForms collection when you can access it directly using its key (ie the form's name)?

If you have hundreds of forms in your db this loop seems very inefficient, especially as it's unnecessary.

At least you should Exit For after you hit the required form, since all subsequent forms in the collection are not the one you are interested in.
 
Just beware of the fact that this expression...
Code:
SysCmd(acSysCmdGetObjectState, acForm, "NameOfForm") = acObjStateOpen
...will return False if the form is open, but contains an unsaved record.

: )
in my case doesn't matter, all i am doing is for forms that are either a menu, or forms that contain no new data, so I am safe.
 
Code:
Public Function FormIsLoaded(sFormName$) As Boolean
' Determines if the form specified in argument <sFormName> is loaded (in any view except Design)
' -------------------------------------------------------------------------------------------------/
On Error GoTo FormIsLoaded_Err

    If Forms(sFormName).CurrentView > 0 Then FormIsLoaded = True '  0 = Design view

FormIsLoaded_Bye:
    Exit Function

FormIsLoaded_Err:
    Err.Clear
End Function
 
Code:
Public Function FormIsLoaded(sFormName$) As Boolean
' Determines if the form specified in argument <sFormName> is loaded (in any view except Design)
' -------------------------------------------------------------------------------------------------/
On Error GoTo FormIsLoaded_Err

    If Forms(sFormName).CurrentView > 0 Then FormIsLoaded = True '  0 = Design view

FormIsLoaded_Bye:
    Exit Function

FormIsLoaded_Err:
    Err.Clear
End Function
And this is why I Love this forums, there is always someone with a different approach, a different solution, this is great..
Thanks
 

Users who are viewing this thread

Back
Top Bottom