squirrel
squirrel

Reputation: 1

Finding out if a one of several forms are open in MS Access

I need to iterate through a list of forms to see if they are open and then do something with them if they are. The following works:

Public Sub isloadedtester()
    Dim iForm As Variant
    
    For Each iForm In CurrentProject.AllForms
        Debug.Print iForm.name & ": " & CurrentProject.AllForms(iForm.name).IsLoaded
    Next
    
End Sub 

but it loops through all forms. So I thought the following should work to loop through only the forms I care about:

Public Sub isloadedtester2()
    Dim iForm As Variant
    Dim list
    
    list = Array(Form_some, Form_another)
    
    For Each iForm In list
        Debug.Print iForm.name
        Debug.Print ".isloaded: " & CurrentProject.AllForms(iForm.name).IsLoaded
        Debug.Print "direct: " & SysCmd(acSysCmdGetObjectState, acForm, iForm.name)
        Debug.Print "by fn: " & IsLoaded(iForm.name)
    Next
    
End Sub
Public Function IsLoaded(FormName As String, Optional aType As AcObjectType = acForm)

    IsLoaded = (SysCmd(acSysCmdGetObjectState, aType, FormName) <> 0)

End Function

However, the second version always sees forms as loaded, no matter which of several ways to read loaded state I use:

.isloaded: True
direct: 1
by fn: True

It's almost like assigning a form to an array makes VBA load the form.

Should I use something other than an array for this? I realize I could do this as an array of names that I also loop through to see if one fits, but that seems awkward, so I first wanted to see if I am just doing something slightly different than it needs to be.

Upvotes: 0

Views: 897

Answers (2)

AVG
AVG

Reputation: 1462

If you only want open forms, then use the Forms collection. It includes only open forms

Dim intFrm As Integer
  If Forms.Count > 0 Then
  For intFrm = 0 To Forms.Count - 1
     'Debug.Print Forms(intFrm).NAME
      If Forms(intFrm).Name IsInYourList Then
        DoSomething
      End IF
  Next intFrm
End If

Upvotes: 1

Andre
Andre

Reputation: 27644

If you have a list of form names, you can do it the "old-fashioned" way with SysCmd

Public Function IsLoaded(FName As String, Optional aType As AcObjectType = acForm)

    IsLoaded = (SysCmd(acSysCmdGetObjectState, aType, FName) <> 0)

End Function

and

For Each iForm In list
    Debug.Print iForm.Name & ": " & IsLoaded(iForm.Name)
Next

Upvotes: 0

Related Questions