Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Is a form open or not

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
DandL
Asked:
DandL
  • 3
  • 3
  • 2
  • +7
1 Solution
 
JonFish85Commented:
if frm.visible = true then
'form is visible
end if
0
 
deightonCommented:
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
 
Arthur_WoodCommented:
Deighton, that will only determine if the form is loaded, not if it is also SHOWN.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Dave_GreeneCommented:

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
 
DennisBorgCommented:
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
 
Arthur_WoodCommented:
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
 
hongjunCommented:
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
 
DandLAuthor Commented:
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
 
deightonCommented:
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
 
DennisBorgCommented:
>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
 
vikleleCommented:
I was also wondering why use of "Exit For" is disallowed ... I can under barring using of Goto execpt for error handling.

0
 
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
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
 
Ryan ChongCommented:
Hi ,

Can we try:

If myForm is Nothing then
 ..
End if

?
0
 
Arthur_WoodCommented:
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
 
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
I fully appreciate that Arthur (I use it myself.) I just wondered if this was why there was a threat of being shot.
0
 
deightonCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
  • +7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now