Link to home
Start Free TrialLog in
Avatar of NNOAM1
NNOAM1Flag for Israel

asked on

know if an Access form is loaded

I have always seen this code for detecting if a form is loaded:

    Dim blMyFormLoaded As Boolean
    Set dbs = Application.CurrentProject    
    For Each obj In dbs.AllForms
        If obj.IsLoaded = True And obj.Name = "[put_here_the_desired_form-name]" Then
            blMyFormLoaded = True
          Exit For
        End If
    Next obj

But I've come up with this Idea:

    Dim blMyFormLoaded As Boolean
    Dim f As Form
    For Each f In Application.Forms
      If f.Name = "[put_here_the_desired_form-name]" And f.CurrentView<>0 then
           blMyFormLoaded = True
             Exit For
      End If
    Next

Doesn't it seem that the second is better? The Forms collection contains only open forms, while the AllForms collection contains all of them. Consider the fact that my app has got 666 forms.
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Well, try and click a stopwatch ...

Under normal use most apps have zero forms open in designview.

/gustav
Avatar of NNOAM1

ASKER

OK, let's remove the: "And f.CurrentView<>0". Are both methods OK with you?
Yes, but I think I would do:
    Dim blMyFormLoaded As Boolean
    Set dbs = Application.CurrentProject    
    For Each obj In dbs.AllForms
      If obj.IsLoaded = True Then
        If obj.Name = "[put_here_the_desired_form-name]" Then
          blMyFormLoaded = True
          Exit For
        End If
      End If
    Next obj


    Dim blMyFormLoaded As Boolean
    Dim f As Form
    For Each f In Application.Forms
      If f.Name = "[put_here_the_desired_form-name]" Then
        If f.CurrentView <> 0 Then
          blMyFormLoaded = True
          Exit For
        End If
      End If
    Next

Open in new window


/gustav
ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NNOAM1

ASKER

That's the thing I was looking for! Thank you.