Solved

Is a form open or not

Posted on 2001-07-27
16
198 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
ID: 6327110
if frm.visible = true then
'form is visible
end if
0
 
LVL 18

Expert Comment

by:deighton
ID: 6327124
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
ID: 6327140
Deighton, that will only determine if the form is loaded, not if it is also SHOWN.
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6327152

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
ID: 6327153
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
ID: 6327160
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
ID: 6327175
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
ID: 6327196
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:deighton
ID: 6327207
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
ID: 6327471
>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
ID: 6329230
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
ID: 6329407
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
ID: 6332690
Hi ,

Can we try:

If myForm is Nothing then
 ..
End if

?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6333785
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
ID: 6333852
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
ID: 6336963
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DO Loop not working 4 72
VBA saving file message display 5 56
TT Auto Dashboard 13 82
VB 6 error 5 in windows 10 but not in XP 7 43
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

912 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

20 Experts available now in Live!

Get 1:1 Help Now