Solved

Is a form open or not

Posted on 2001-07-27
16
197 Views
Last Modified: 2011-08-08
How do you check to see if a form is open?  Without setting boolean flags.

like :
frm.show
boolFlagfrm = true
.
.
.
.
if(boolFlagfrm= true)then
    unload frm
end if

0
Comment
Question by:DandL
  • 3
  • 3
  • 2
  • +7
16 Comments
 
LVL 6

Accepted Solution

by:
JonFish85 earned 25 total points
Comment Utility
if frm.visible = true then
'form is visible
end if
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
Public Function IsFormLoaded(sForm As String) As Boolean


Dim frmX As Form
Dim sUForm As String

sUForm = UCase(sForm)

For Each frmX In Forms
    If UCase(frmX.Name) = sUForm Then IsFormLoaded = True
Next


End Function

e.g.

isformloaded("frmMain")
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
Deighton, that will only determine if the form is loaded, not if it is also SHOWN.
0
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility

Private Sub Command1_Click()
  Dim i As Integer
  Dim frmX As Form
  For i = 0 To Forms.Count - 1
    Set frmX = Forms.Item(i)
    If frmX.Name = "Form2" Then
       If frmX.Visible then
         MsgBox "Form2 Shown"
       End if
    End If
  Next
End Sub
0
 
LVL 8

Expert Comment

by:DennisBorg
Comment Utility
As an assist, here is modified version of deighton's routine which is optimized a little:

Public Function IsFormLoaded(sForm As String) As Boolean
   Dim frmX As Form

   For Each frmX In Forms
      If StrComp(frmX.Name,sForm,vbTextCompare) = 0 Then
         IsFormLoaded = True
         Exit For
      End If
      Set frmX = Nothing
   Next
End Function

0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
Dandl,
   Why are you concerned if the form is Showing?

You can easily Unload ALL Loaded forms:

dim frm as Form
   
for each frm in Forms
    frm.Unload
Next

That will cleanly Unload ALL Loaded forms.
0
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
See below function

' See if a Form is Loaded
' You can use the Forms collection to see if a form with a certain name is
' loaded. Loop through the forms comparing their names to the target name.

' Return a form by name if it is loaded.
Private Function FindForm(ByVal form_name As String) As Boolean
     Dim i As Integer

        ' Assume we will not find it.
        FindForm = False

        ' Search the loaded forms.
        For i = 0 To Forms.Count - 1
            If Forms(i).Name = form_name Then
                ' We found it. Return this form.
                FindForm = True
                Exit For
            End If
        Next i
End Function


hongjun
0
 
LVL 1

Author Comment

by:DandL
Comment Utility
okay that is a lot of info....give me some time to wade through it all.  I think y'all have brought up an issue or two that I had not thought about. Like if the form is load or if it is visable.

Arthur_Wood you made a good point about
>Why are you concerned if the form is Showing?
I think that might be the real issue....I can just always unload it.
Humm..
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 18

Expert Comment

by:deighton
Comment Utility
In my app I hide form A then show form B, but sometimes I have only shown form B - so I need to know if Form A is loaded.

DennisBorg,

I agree the 'exit for' is a good idea, but I get shot if I use it her :->
0
 
LVL 8

Expert Comment

by:DennisBorg
Comment Utility
>I agree the 'exit for' is a good idea, but I get shot if
>I use it her :->

Who would shoot you, and why? :-)


-Dennis Borg
0
 

Expert Comment

by:viklele
Comment Utility
I was also wondering why use of "Exit For" is disallowed ... I can under barring using of Goto execpt for error handling.

0
 
LVL 17
Comment Utility
I think that a For loop is meant to complete it's iterations. If you say For x = 1 To 10 then it is expected to loop 10 times. To exit before this number would be wrong.
I also feel that Exit Do should be wrong unless the loop condition has been met.
However you need to get out of the thing sometimes unless your code design is perfect and I have not found this 'perfect' answer yet. :7)
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Hi ,

Can we try:

If myForm is Nothing then
 ..
End if

?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
RobinD
   The use of Exit For is to allow a For...Next loop to termnate at the appropriate point  ->  if you are looping through a collection, in order to find a certain entry , once you have found it, searching further is a waste of CPU cycles, so the Exit For allows you to get on with the processing, rather than spining the extra cycles:

   For each frm in Forms
      if frm.Name = "MyForm"
         frm.Hide
         Exit FOr
      End If
   Next frm

will look for the Form named "MyForm", Hide it, and then exit the loop---> if you had 10 forms, and MyForm happened to be #2, then you find it, exit the loop, and don't waste the time or cycles looking at the remaing 8, which ARE NOT named "MyForm".


It is NOT ALWAYS the case that a For...Next loop is intended to carry out ALL of the indicated iterations.

0
 
LVL 17
Comment Utility
I fully appreciate that Arthur (I use it myself.) I just wondered if this was why there was a threat of being shot.
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
RobinD - my superior does not like exit for, he thinks it is a 'goto' statement - I have been conditioned not to use it, but it does increase program efficiency - and leaves you with a record of the final value of c.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now