Solved

Is a form open or not

Posted on 2001-07-27
16
201 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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 51

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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